Requests模块是第三方模块,需要预先安装,requests模块在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得更加简洁和人性化。
一、get请求
1.1 发送无参数的get请求,尝试获取某个网页.
发送无参数的get请求
r = requests.get('http://www.baidu.com')
发送无参数的get请求 设置超时时间 timeout 单位秒
r = requests.get('http://www.baidu.com', timeout=1)
1.2 发送带参数的请求
你也许经常想为 URL 的查询字符串(query string) 传递某种数据。如果你是手工构建 URL,那么数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。例如, www.baidu.com/?key=val。 Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数。举例来说,如果你想传递 key1=value1 和 key2=value2 到 www.baidu.com/ ,那么你可以使用如下代码:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("https://www.baidu.com/", params=payload)
print(r.url)
https://www.baidu.com/?key2=value2&key1=value1
你还可以将一个列表作为值传入
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get('http://www.baidu.com/', params=payload)
print(r.url)
http://www.baidu.com/?key2=value2&key2=value3&key1=value1
如果你想为请求添加 HTTP 头部,只要简单地传递一个 dict 给 headers 参数就可以了
url = 'https://www.baidu.com/s?wd=python'
headers = {
'Content-Type': 'text/html;charset=utf-8',
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
r = requests.get(url,headers=headers)
1.3、查看响应信息
r.headers #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None r.status_code #返回连接状态,200正常。 r.text #默认以unicode形式返回网页内容,也就是网页源码的字符串。 r.content #以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。 r.json() #把网页中的json数据转成字典并将其返回。 r.encoding #获取当前的编码 r.encoding = 'ISO-8859-1' #指定编码,r.text返回的数据类型,写在r.text之前。
1.4、查看请求信息
r.url #打印输出该 URL
二、post请求
HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式,服务端通过是根据请求头中的Content-Type字段来获知请求中的消息主体是用何种方式进行编码,再对消息主体进行解析。具体的编码方式包括
以form表单形式提交数据
application/x-www-form-urlencoded
以json串提交数据
application/json
一般使用来上传文件
multipart/form-data
2.1、以form形式发送post请求
Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可
payload = {'key1': 'value1',
'key2': 'value2'
}
r = requests.post("http://httpbin.org/post", data=payload)
查看响应信息
>>> print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.25.1",
"X-Amzn-Trace-Id": "Root=1-619c74b9-1d1a4e3966a4c9c21890086f"
},
"json": null,
"origin": "218.241.217.42",
"url": "http://httpbin.org/post"
}
查看请求头
>>> print(r.request.headers)
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '23', 'Content-Type': 'application/x-www-form-urlencoded'}
>>>
查看请求body
>>> print(r.request.body) key1=value1&key2=value2
2.2、以json形式发送post请求
可以将一 json串传给requests.post()的data参数
url = 'http://httpbin.org/post'
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=json.dumps(payload))
>>> print(r2.text)
{
“args”: {},
“data”: “{/”key1/”: /”value1/”, /”key2/”: /”value2/”}”,
“files”: {},
“form”: {},
“headers”: {
“Accept”: “*/*”,
“Accept-Encoding”: “gzip, deflate”,
“Content-Length”: “36”,
“Host”: “httpbin.org”,
“User-Agent”: “python-requests/2.25.1”,
“X-Amzn-Trace-Id”: “Root=1-619c75ff-03ebfd6964393e387ebf527c”
},
“json”: {
“key1”: “value1”,
“key2”: “value2”
},
“origin”: “218.241.217.42”,
“url”: “http://httpbin.org/post”
}
查看请求头和请求body
>>> print(r2.request.headers)
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '36'}
>>>
>>> print(r2.request.body)
{"key1": "value1", "key2": "value2"}
2.3、以multipart形式发送post请求
Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files参数即可
>>> url = 'http://httpbin.org/post'
>>> files = {'file':open('fang.txt','rb')}
>>> r3 = requests.post(url,files=files)
>>> r3.request.headers
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '164', 'Content-Type': 'multipart/form-data; boundary=5618f94218afb024124625245ed0df12'}
>>>
>>> r3.request.body
b'--5618f94218afb024124625245ed0df12/r/nContent-Disposition: form-data; name="file"; filename="wang.txt"/r/n/r/n//[System//] /r/n1234 /r/n/r/n--5618f94218afb024124625245ed0df12--/r/n'
三、实例
3.1、下载单张图片并保存到本地磁盘目录.
#!/usr/bin/env python
#coding:utf-8
import requests
import os
# 下载图片URL
url = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'
# 保存地址
path = "E://图片//"
# 构造下载图片url
down = path + url.split('/')[-1]
#print(down)
# E://图片//bd_logo1_31bdc765.png
try:
# 判断目录是否存在
if not os.path.exists(path):
os.mkdir(path)
# 如果url不存在,则开始下载
if not os.path.exists(down):
r = requests.get(url)
print(r)
# 开始写文件,wb代表写二进制文件
with open(down,'wb') as f:
# 图片以二进制形式保存(r.content)
f.write(r.content)
print("图片下载成功")
else:
print("图片已经存在.")
except Exception as e :
print("爬取失败:",str(e))
3.2、使用requests模块和bs4模块,抓取贴吧图片并保存到指定目录
#!/usr/bin/env python
#coding:utf-8
import requests
from bs4 import BeautifulSoup
import os
#图片保存路径:
path = "E://爬虫专用//"
URL = 'http://tieba.baidu.com/p/1753935195'
html_page = requests.get(URL)
#创建BeautifulSoup对象
soup = BeautifulSoup(html_page.text,'html.parser')
#通过class="BDE_Image"获取所有的img 标签
class_image = soup.findAll(attrs={"class":"BDE_Image"})
print(class_image)
#判断目录是否存在
if not os.path.exists(path):
os.mkdir(path)
try:
x = 0
# 循环class_image列表,找到所有img标签的链接
for i in class_image:
#取出src对应的url地址
src_url = i.get('src')
#请求src_url链接地址
imge_list = requests.get(src_url)
#构造url名称
#down = path + src_url.split('/')[-1]
down = path + '%s.jpg' %x
print(down)
#以二进制保存图片
with open(down,'wb') as f:
f.write(imge_list.content)
x += 1
except Exception as e:
print("pass")
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/272159.html