python爬虫requests的使用
1 发送get请求获取页面
import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)
2 发送post请求获取页面
import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.post(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)
3 伪装浏览器,携带报头
import requests # 伪装我们的报文头,加上Use-Agent 伪装成浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
4 携带数据 (比如 发送请求去登陆)
import requests # 如果伪装登录,可以传送一个字典类型数据
data = {
'''这里放入需要的key:value'''
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
# get请求用params 相当于在url后面拼接key=value&key=value
response = requests.get(url=url,params=data)
# post用data传入参数 携带post的数据
response = requests.post(url=url,data=data)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
5 代理
import requests
# 将代理的服务器放入这里,key为协议类型 value为代理的ip和端口
# 发送https或者http请求会根据不同代理ip选择 为我们发送请求
proxies = {
'http':'http://127.0.0.1:80',
'https':'https://127.0.0.1:80'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,proxies=proxies)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
6 携带cookie
import requests # 如果要带着cookie字典 可以传入cookie,也可以放在报文头当中
cookies = {
#'key':'value',
} # 或者将cookie放在报文头当中
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,cookies=cookies)
#response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
7 保持session 帮我们保存response中的session
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url)
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
8 设置连接超时时间
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,timeout = 3) # 3秒时间为超时时间
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
9 设置ssl校验 对方https协议合法性是否忽略
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,verify=False) # 不校验ssl 如果对方https协议不合法,我们忽略 继续请求
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
10 重新连接次数
import requests
from retrying import retry @retry(stop_max_attempt_number=3) # 设置超时重新连接 次数3
def get( url ):
response = requests.get(url=url,timeout=3)
return response.content.decode() url = 'http://www.baidu.com'
html = get(url)
print(html)
python爬虫requests的使用的更多相关文章
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- Python 爬虫—— requests BeautifulSoup
本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...
- Python爬虫--Requests库
Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests是python实现的最简单易用的HTTP库, ...
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
[写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...
- Python爬虫 requests库基础
requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...
- python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例
第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码low,效率差,也没有保存到本地文件或者数据库,强行使用了一波多线程导致数据顺序发生了变化... 贴在这里,引以为戒吧. # -*- coding: ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
- Python爬虫---requests库快速上手
一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...
- Python爬虫requests判断请求超时并重新发送请求
下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html 在爬虫的执行当中,总会遇到请求连接 ...
- python爬虫——requests库使用代理
在看这篇文章之前,需要大家掌握的知识技能: python基础 html基础 http状态码 让我们看看这篇文章中有哪些知识点: get方法 post方法 header参数,模拟用户 data参数,提交 ...
随机推荐
- Jmeter + Ant 测试环境搭建 及解决问题: the <jmeter> type doesn't support nested text data
1.首先确保测试机器中已经按照jdk1.6以上版本,如果没有,那就上官网下载吧. 2.下载Ant,解压至指定目录,并配置好环境变量:http://ant.apache.org/ 在命令行下执行ant ...
- READ TABLE 的用法
SORT ITAB BY '你想比较的列'. " 排序以增加二分查找的速度 READ TABLE itab with key 'itab中某列' = ‘目标列' BINARY SEARCH. ...
- python编程中的if __name__ == 'main与windows中使用多进程
if __name__ == 'main 一个python的文件有两种使用的方法,第一是直接作为程序执行,第二是import到其他的python程序中被调用(模块重用)执行. 因此if __name_ ...
- angular的$scope的使用
1. 可以在scope中直接使用 // 监听日期变化 $scope.$watch('vaFilter.startEffectiveDate', function(newDate, oldDate, s ...
- Dubbo学习1-Hello world
前言 互联网技术到今天已经非常成熟和稳定了,其中为了解决高并发.大规模的服务请求,出现了微服务.RPC这样的分布式架构.今天就从头开始学习RPC框架dubbo. 为什么要学Dubbo 关于分布式的解决 ...
- cloneNode和replaceChild
node.cloneNode(deep) var node=document.getElementById("myList2").lastChild.cloneNode(true) ...
- 【Android】带进度条的WebView
http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
- Springmvc 视频学习地址
http://www.icoolxue.com/album/show/216/
- iOS App 启动性能优化
1. App启动过程 解析Info.plist 加载相关信息,例如如闪屏 沙箱建立.权限检查 Mach-O加载 如果是胖二进制文件,寻找合适当前CPU类别的部分 加载所有依赖的Mach-O文件(递归调 ...
- java使用io创建文件与删除文件的工具类
java中对于文件的操作,是再常见不过了.以下代码是自己代码中所用到的工具类,仅供参考. import java.io.File; import java.io.IOException; /** * ...