本文主要是介绍爬虫第四天 ---requests库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Requests:让HTTP服务人类
就这一句话,可见requests
库有多么的强大了。今天我们来简单的了解一下。
requests库
安装和文档地址
利用pip
安装:
pip install requests
中文文档:
https://2.python-requests.org//zh_CN/latest/user/quickstart.html
发送get请求
1.最简单的发送get
请求就是通过requests.get
来调用。
import requests
response = requests.get("https://ww.baidu.com/")
print(response) # 返回的是状态码 <Response [200]>
print(type(response.text)) #<class 'str'>
print(response.text)
print(type(response.content)) #<class 'bytes'>
print(response.content.decode('utf-8'))
2.response.text
和response.content
的区别
1. response.text
:数据类型是str
,是resquests
库将resquests.content
进行解码的字符串。解码需要指定一个编码方式,resuqests
会根据自己的猜测来判断编码的方式。所以有时候可能会猜错,就会导致解码产生乱码。这时候就应该使用response.content.decode('utf-8')
进行手动解码。
2. response.content
这个是直接从网上抓取数据。没有经过任何解码。所以是一个bytes
类型。在硬盘和网络上传输的字符串都是bytes
类型。
3.添加headers
和查询参数。
如果想添加headers
,可以传入headers参数来增加请求头中的headers
信息。如果要将参数放在url中传递,可以利用params
参数。
import requests
kw = {"wd":"python"}
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Mobile Safari/537.36'
}
# params接收一个字典或字符串,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("https://ww.baidu.com/",params=kw,headers=headers)
#查看响应内容,返回的是Unicode格式数据
print(response.text)#查看响应内容,response.content 返回字节流数据
print(response.content)#查看url地址
print(response.url) #'https://www.baidu.com/?wd=python'#查看响应头部字符编码
print(response.encoding) # 'utf-8'#查看响应码
print(response.status_code) #200
发送post
请求
发送post请求,直接调用requests.post
方法就可以了。如果返回的是json数据,那么可以调用response.json
将json 字符串转换为字典或列表。
import requests
url = "https://www.lagou.com/jobs/list_python"
data = {'first': 'true','pn': '1','kd': 'python'
}
headers = {'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=','User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Mobile Safari/537.36'
}
response = requests.post(url,data=data,headers=headers)print(response.json())
print(type(response.json()))
#报错 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
#在网上查半天也没弄出来,还是不理解啊
代理
在请求方法中,传递proxies
参数就可以了。
import requests
proxy = {'http':'136.228.128.14:61158'
}
response = requests.get('http://httpbin.org/ip',proxies=proxy)
print(response.text)
"""
不使用代理网址
{"origin": "58.57.75.158, 58.57.75.158"
}使用代理网址
{"origin": "136.228.128.14, 136.228.128.14"
}
"""
cookie
在一个响应中包含了cookie
,那么可以利用cookies
属性拿到这个返回的 cookie
值。
import requests
url = "http://www.renren.com/PLogin.do"
data = {'email':'2535455833@qq.com','password':'2535455833'
}
response = requests.get('http://www.baidu.com/')
print(response.cookies)
print(response.cookies.get_dict())
session
如果想要在多次请求中共享cookie
,那么就应该使用session
。
import requests
url = "http://www.renren.com/PLogin.do"
data = {'email':'2535455833@qq.com','password':'2535455833'
}
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Mobile Safari/537.36'
}
session = requests.Session()
session.post(url,data=data,headers=headers)
response = session.get("http://www.renren.com/880151247/profile")
with open('renren.html','w',encoding='utf-8') as f:f.write(response.text)
SSL证书
处理不信任的SSL
证书: 在requests.get
里有参数verify=False
即可。
这篇关于爬虫第四天 ---requests库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!