如何使用request库进行Python爬虫的基本操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计757个文字,预计阅读时间需要4分钟。
目录:request.get() 方法- 添加参数:使用 param 字典- 添加头部:使用 headers 字典- 高级用法: - Cookies:维持会话 Session - SSL证书验证 - 代理设置 - 超时设置 - 身份验证:使用 request 处理网页验证
目录- request
- get()
- 添加参数使用param+字典
- 添加headers使用headers+字典
- 高级用法
- Cookies
- 维持会话Session()
- SSL证书验证
- 代理设置
- 超时设置
- 身份认证
用urllib去处理网页验证和Cookies时,需要写Opener和Handler来处理,很不方便,这里我们学习更为强大的库request
get()实例:
import requests #导入requests html = requests.get('www.csdn.net/')#使用get方法获取页面信息 print(html.text)#调取text属性查看页面代码
添加参数使用param+字典import requests # 导入requests data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.get('sou.zhaopin.com/',params=data) # 添加参数 print(html.text) # 调取text属性查看页面代码
添加headers使用headers+字典import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.get('sou.zhaopin.com/',headers=headers,params=data) # 添加参数 print(html.text) # 调取text属性查看页面代码
高级用法cookies设置,代理设置等
Cookies获取cookies:
import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.get('blog.csdn.net/qq_40966461/article/details/104974998',headers=headers,params=data) # 添加参数 print(html.cookies) # 调取text属性查看页面代码 for key,value in html.cookies.items(): print(key+'='+value)
很简单,直接获取cookies属性即可
维持会话Session()在requests中,如果直接利用get()或post()等方法可以做到模拟网页的请求,但是这实际上时相当于不同的会话,相当于用了两个浏览器打开了不同的页面,这时需要用session对象来维护对话
import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.Session().get('blog.csdn.net/qq_40966461/article/details/104974998',headers=headers,params=data) # 添加参数 print(html.cookies) # 调取text属性查看页面代码 for key,value in html.cookies.items(): print(key+'='+value)
调用requests模块中get方法时先创建一个Session对象
SSL证书验证import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } response = requests.get('www.12306.cn',headers=headers,verify = False) print(response.status_code)
verify=False即可
代理设置import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } proxies = { "183.166.132.176", "183.166.132.176" } response = requests.get('www.12306.cn',headers=headers,proxies=proxies,verify = False) print(response.status_code)
添加proxies即可,代理可以搜索快代理
超时设置加参数timeout= 1
身份认证get中添加参数 auth=(‘username’,‘password’)
OAuth认证方式
到此这篇关于Python爬虫基本库request的基本使用的文章就介绍到这了,更多相关Python爬虫request库内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计757个文字,预计阅读时间需要4分钟。
目录:request.get() 方法- 添加参数:使用 param 字典- 添加头部:使用 headers 字典- 高级用法: - Cookies:维持会话 Session - SSL证书验证 - 代理设置 - 超时设置 - 身份验证:使用 request 处理网页验证
目录- request
- get()
- 添加参数使用param+字典
- 添加headers使用headers+字典
- 高级用法
- Cookies
- 维持会话Session()
- SSL证书验证
- 代理设置
- 超时设置
- 身份认证
用urllib去处理网页验证和Cookies时,需要写Opener和Handler来处理,很不方便,这里我们学习更为强大的库request
get()实例:
import requests #导入requests html = requests.get('www.csdn.net/')#使用get方法获取页面信息 print(html.text)#调取text属性查看页面代码
添加参数使用param+字典import requests # 导入requests data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.get('sou.zhaopin.com/',params=data) # 添加参数 print(html.text) # 调取text属性查看页面代码
添加headers使用headers+字典import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.get('sou.zhaopin.com/',headers=headers,params=data) # 添加参数 print(html.text) # 调取text属性查看页面代码
高级用法cookies设置,代理设置等
Cookies获取cookies:
import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.get('blog.csdn.net/qq_40966461/article/details/104974998',headers=headers,params=data) # 添加参数 print(html.cookies) # 调取text属性查看页面代码 for key,value in html.cookies.items(): print(key+'='+value)
很简单,直接获取cookies属性即可
维持会话Session()在requests中,如果直接利用get()或post()等方法可以做到模拟网页的请求,但是这实际上时相当于不同的会话,相当于用了两个浏览器打开了不同的页面,这时需要用session对象来维护对话
import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } data = { 'jl': '765', 'kw': 'python', 'kt': '3' } html = requests.Session().get('blog.csdn.net/qq_40966461/article/details/104974998',headers=headers,params=data) # 添加参数 print(html.cookies) # 调取text属性查看页面代码 for key,value in html.cookies.items(): print(key+'='+value)
调用requests模块中get方法时先创建一个Session对象
SSL证书验证import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } response = requests.get('www.12306.cn',headers=headers,verify = False) print(response.status_code)
verify=False即可
代理设置import requests # 导入requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } proxies = { "183.166.132.176", "183.166.132.176" } response = requests.get('www.12306.cn',headers=headers,proxies=proxies,verify = False) print(response.status_code)
添加proxies即可,代理可以搜索快代理
超时设置加参数timeout= 1
身份认证get中添加参数 auth=(‘username’,‘password’)
OAuth认证方式
到此这篇关于Python爬虫基本库request的基本使用的文章就介绍到这了,更多相关Python爬虫request库内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

