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解析html开发库pyquery使用方法
Feb 07 Python
Python编写检测数据库SA用户的方法
Jul 11 Python
Python实现方便使用的级联进度信息实例
May 05 Python
利用Python如何生成hash值示例详解
Dec 20 Python
django在接受post请求时显示403forbidden实例解析
Jan 25 Python
Python Web编程之WSGI协议简介
Jul 18 Python
从运行效率与开发效率比较Python和C++
Dec 14 Python
使用Python 正则匹配两个特定字符之间的字符方法
Dec 24 Python
在Python中,不用while和for循环遍历列表的实例
Feb 20 Python
详解Python_shutil模块
Mar 15 Python
Python3安装psycopy2以及遇到问题解决方法
Jul 03 Python
Python高级编程之继承问题详解(super与mro)
Nov 19 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取整数函数常用的四种方法小结
2012/07/05 PHP
实例讲解YII2中多表关联的使用方法
2017/07/21 PHP
KindEditor在php环境下上传图片功能集成的方法示例
2020/07/20 PHP
PHP7 其他语言层面的修改
2021/03/09 PHP
强制设为首页代码
2006/06/19 Javascript
JavaScript通过正则表达式实现表单验证电话号码
2014/03/07 Javascript
js中的cookie的读写操作示例详解
2014/04/17 Javascript
浅谈javascript中createElement事件
2014/12/05 Javascript
深入理解JavaScript系列(21):S.O.L.I.D五大原则之接口隔离原则ISP详解
2015/03/05 Javascript
javascript十六进制及二进制转化的方法
2015/05/06 Javascript
异步加载JS、CSS代码(推荐)
2016/06/15 Javascript
prototype.js常用函数详解
2016/06/18 Javascript
JS实现自动阅读单词(有道单词本添加功能)
2016/11/14 Javascript
微信小程序之picker日期和时间选择器
2017/02/09 Javascript
vue封装第三方插件并发布到npm的方法
2017/09/25 Javascript
jquery使用iscorll实现上拉、下拉加载刷新
2017/10/26 jQuery
js Element Traversal规范中的元素遍历方法
2018/04/19 Javascript
Angularjs中的$apply及优化使用详解
2018/07/02 Javascript
vue将毫秒数转化为正常日期格式的实例
2018/09/16 Javascript
详解在IDEA中将Echarts引入web两种方式(使用js文件和maven的依赖导入)
2020/07/11 Javascript
vue单元格多列合并的实现
2020/11/26 Vue.js
[05:56]第十六期——新进3大C之小兔基
2014/06/24 DOTA
[01:03:22]LGD vs OG 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
Django2.1集成xadmin管理后台所遇到的错误集锦(填坑)
2018/12/20 Python
详解pandas如何去掉、过滤数据集中的某些值或者某些行?
2019/05/15 Python
python3的数据类型及数据类型转换实例详解
2019/08/20 Python
解决tensorflow训练时内存持续增加并占满的问题
2020/01/19 Python
Python爬虫爬取糗事百科段子实例分享
2020/07/31 Python
HTML5+CSS3绘制锯齿状的矩形
2016/03/01 HTML / CSS
佳能德国网上商店:Canon德国
2017/03/18 全球购物
德国街头和运动文化高品质商店:BSTN Store
2017/08/26 全球购物
The North Face北面法国官网:美国著名户外品牌
2019/11/01 全球购物
美国婴儿服装购物网站:Gerber Childrenswear
2020/05/06 全球购物
护理专业应届毕业生推荐信
2013/11/15 职场文书
Python利用zhdate模块实现农历日期处理
2022/03/31 Python
python中mongodb包操作数据库
2022/04/19 Python