1. requests基本用法
- 请求网络数据:
request.get
(请求地址) - 设置解码方式(乱码的需要设置 - 一定要在获取请求结果之前设置)
- 获取请求结果
- 获取请求结果对应的文本数据 - 爬网页
- 获取二进制格式的请求结果 - 下载图片、视频、音频
- 获取请求结果
json
转换的结果 -json
接口
import requests
url = 'https://cd.zu.ke.com/zufang'
response = requests.get(url)
response.encoding = 'utf-8' # charset的值
print(response.text)
print(response.content)
print(response.json())
2. 添加请求头
-
发送请求
添加
header
:a. 浏览器伪装(user-agent
)、b. 免密登录(cookie
)、c. 设置代理(proxies
) -
获取结果
headers = {
'cookie': 'bid=6_8dxUfYYkU; ll="118318"; __yadk_uid=6vbmOO8lS139b1tQoeIN9oAAVT0f9Smj; __gads=ID=49cc1b85c91f6d10-22ccc45103da00ec:T=1676961329:RT=1676961329:S=ALNI_MZTKMmxHVCcUFlEWVNlpfasapW4VA; _vwo_uuid_v2=D1BCDB1CCECC51845D8A160DF862B7404|0526c336d00b44b9679db3f86b2daed3; _pk_ref.100001.4cf6=["","",1679904257,"https://www.baidu.com/link?url=G_L4Gz7745ieRra1iaVJghhm_XqoDkUZ0KzIZQ1E2K2sfNIGmiyj3yoaxa-D8eD2&wd=&eqid=bcf6e0a9000a83ff0000000664214dfe"]; _pk_id.100001.4cf6=def380ad005c9696.1676961322.2.1679904257.1676961509.; _pk_ses.100001.4cf6=*; ap_v=0,6.0; __utma=30149280.1144007092.1676961323.1676961323.1679904259.2; __utmb=30149280.0.10.1679904259; __utmc=30149280; __utmz=30149280.1679904259.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utma=223695111.1828244816.1676961323.1676961323.1679904259.2; __utmb=223695111.0.10.1679904259; __utmc=223695111; __utmz=223695111.1679904259.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __gpi=UID=00000bc7baa18cef:T=1676961329:RT=1679904262:S=ALNI_Mb56q-yMSjp0Sjt4BR1wgp3_MYyNQ',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
url = 'https://movie.douban.com/top250'
response = requests.get(url, headers=headers)
result = response.text
print(result)
3. 下载图片
import requests
# 1. 获取网络图片数据
url = 'https://cn.bing.com/images/search?view=detailV2&ccid=dqTOXvN2&id=0FA019CF5408A58E0E3367C4FF19A19451BA2340&thid=OIP.dqTOXvN2dT16WOO_mXNNXgHaEK&mediaurl=https%3a%2f%2fwebstatic.mihoyo.com%2fupload%2fstatic-resource%2f2021%2f08%2f13%2f76a4ce5ef376753d7a58e3bf99734d5e_3245229241484943714.jpg&exph=1080&expw=1920&q=%e5%8e%9f%e7%a5%9e&simid=607988660303889133&FORM=IRPRST&ck=7427BCC25E569F6D91B89D20D14D0517&selectedIndex=6&ajaxhist=0&ajaxserp=0'
headers = {
'cookie': 'bid=6_8dxUfYYkU; ll="118318"; __yadk_uid=6vbmOO8lS139b1tQoeIN9oAAVT0f9Smj; __gads=ID=49cc1b85c91f6d10-22ccc45103da00ec:T=1676961329:RT=1676961329:S=ALNI_MZTKMmxHVCcUFlEWVNlpfasapW4VA; _vwo_uuid_v2=D1BCDB1CCECC51845D8A160DF862B7404|0526c336d00b44b9679db3f86b2daed3; _pk_ref.100001.4cf6=["","",1679904257,"https://www.baidu.com/link?url=G_L4Gz7745ieRra1iaVJghhm_XqoDkUZ0KzIZQ1E2K2sfNIGmiyj3yoaxa-D8eD2&wd=&eqid=bcf6e0a9000a83ff0000000664214dfe"]; _pk_id.100001.4cf6=def380ad005c9696.1676961322.2.1679904257.1676961509.; _pk_ses.100001.4cf6=*; ap_v=0,6.0; __utma=30149280.1144007092.1676961323.1676961323.1679904259.2; __utmb=30149280.0.10.1679904259; __utmc=30149280; __utmz=30149280.1679904259.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utma=223695111.1828244816.1676961323.1676961323.1679904259.2; __utmb=223695111.0.10.1679904259; __utmc=223695111; __utmz=223695111.1679904259.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __gpi=UID=00000bc7baa18cef:T=1676961329:RT=1679904262:S=ALNI_Mb56q-yMSjp0Sjt4BR1wgp3_MYyNQ',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
response = requests.get(url)
result = response.content
print(type(result))
# 2. 保存图片数据到本地文件
with open(r'files\a.jpeg', 'wb') as f:
f.write(result)