python爬虫 基于requests模块的get请求实现详解


Posted in Python onAugust 20, 2019

需求:爬取搜狗首页的页面数据

import requests
# 1.指定url
url = 'https://www.sogou.com/'
# 2.发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url=url)
# 3.获取响应中的数据:text属性作用是可以获取响应对象中字符串形式的页面数据
page_data = response.text
# 4.持久化数据
with open("sougou.html","w",encoding="utf-8") as f:
  f.write(page_data)
  f.close()
print("ok")

requests模块如何处理携带参数的get请求,返回携带参数的请求

需求:指定一个词条,获取搜狗搜索结果所对应的页面数据

之前urllib模块处理url上参数有中文的需要处理编码,requests会自动处理url编码

发起带参数的get请求

params可以是传字典或者列表

def get(url, params=None, **kwargs):
  r"""Sends a GET request.
  :param url: URL for the new :class:`Request` object.
  :param params: (optional) Dictionary, list of tuples or bytes to send
    in the body of the :class:`Request`.
  :param \*\*kwargs: Optional arguments that ``request`` takes.
  :return: :class:`Response <Response>` object
  :rtype: requests.Response
import requests
# 指定url
url = 'https://www.sogou.com/web'
# 封装get请求参数
prams = {
  'query':'周杰伦',
  'ie':'utf-8'
}
response = requests.get(url=url,params=prams)
page_text = response.text
with open("周杰伦.html","w",encoding="utf-8") as f:
  f.write(page_text)
  f.close()
print("ok")

利用requests模块自定义请求头信息,并且发起带参数的get请求

get方法有个headers参数 把请求头信息的字典赋给headers参数

import requests
# 指定url
url = 'https://www.sogou.com/web'
# 封装get请求参数
prams = {
  'query':'周杰伦',
  'ie':'utf-8'
}
# 自定义请求头信息
headers={
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
  }
response = requests.get(url=url,params=prams,headers=headers)
page_text = response.text
with open("周杰伦.html","w",encoding="utf-8") as f:
  f.write(page_text)
  f.close()
print("ok")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python操作CouchDB的方法
Oct 08 Python
Python的MongoDB模块PyMongo操作方法集锦
Jan 05 Python
django基础之数据库操作方法(详解)
May 24 Python
浅谈numpy中linspace的用法 (等差数列创建函数)
Jun 07 Python
python中字符串比较使用is、==和cmp()总结
Mar 18 Python
python通过http下载文件的方法详解
Jul 26 Python
Python3 翻转二叉树的实现
Sep 30 Python
python 通过手机号识别出对应的微信性别(实例代码)
Dec 22 Python
OpenCV+python实现实时目标检测功能
Jun 24 Python
Python绘图实现台风路径可视化代码实例
Oct 23 Python
python xlsxwriter模块的使用
Dec 24 Python
用Python进行栅格数据的分区统计和批量提取
May 27 Python
python爬虫 urllib模块url编码处理详解
Aug 20 #Python
pytorch实现用Resnet提取特征并保存为txt文件的方法
Aug 20 #Python
python web框架 django wsgi原理解析
Aug 20 #Python
opencv转换颜色空间更改图片背景
Aug 20 #Python
pytorch 预训练层的使用方法
Aug 20 #Python
python爬虫 urllib模块反爬虫机制UA详解
Aug 20 #Python
Pytorch 抽取vgg各层并进行定制化处理的方法
Aug 20 #Python
You might like
php获取通过http协议post提交过来xml数据及解析xml
2012/12/16 PHP
PHP_Cooikes不同页面无法传递的解决方法
2014/03/07 PHP
PHP结合jQuery实现找回密码
2015/07/22 PHP
关于PHP中协程和阻塞的一些理解与思考
2017/08/11 PHP
js控制CSS样式属性语法对照表
2012/12/11 Javascript
Bootstrap布局方式详解
2016/05/27 Javascript
JavaScript实现通过select标签跳转网页的方法
2016/09/29 Javascript
原生JS实现九宫格抽奖效果
2017/04/01 Javascript
分享十三个最佳JavaScript数据网格库
2017/04/07 Javascript
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
简单实现jquery隔行变色
2017/11/09 jQuery
nodejs连接mysql数据库及基本知识点详解
2018/03/20 NodeJs
Angular6封装http请求的步骤详解
2018/08/13 Javascript
微信小程序实现时间预约功能
2018/11/27 Javascript
Vue递归组件+Vuex开发树形组件Tree--递归组件的简单实现
2019/04/01 Javascript
Python六大开源框架对比
2015/10/19 Python
详解Python的collections模块中的deque双端队列结构
2016/07/07 Python
详解Python 数据库 (sqlite3)应用
2016/12/07 Python
python 禁止函数修改列表的实现方法
2017/08/03 Python
Python实现的多线程同步与互斥锁功能示例
2017/11/30 Python
Python中最大最小赋值小技巧(分享)
2017/12/23 Python
python3下实现搜狗AI API的代码示例
2018/04/10 Python
python异常处理try except过程解析
2020/02/03 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
2020/04/22 Python
关于Python错误重试方法总结
2021/01/03 Python
CSS3 linear-gradient线性渐变生成加号和减号的方法
2017/11/21 HTML / CSS
竞聘医务工作人员的自我评价分享
2013/11/04 职场文书
个人自我评价范文
2014/02/05 职场文书
物理研修随笔感言
2014/02/14 职场文书
个人担保书格式范文
2014/05/12 职场文书
上班时间打瞌睡检讨书
2014/09/26 职场文书
文明单位申报材料
2014/12/23 职场文书
总经理检讨书范文
2015/02/16 职场文书
同意离婚答辩状
2015/05/22 职场文书
入党转正申请自我鉴定
2019/06/25 职场文书
nginx容器方式反向代理实战
2022/04/18 Servers