Day1-requests基础
1. requests基本用法
import requests
response = requests.get('https://cd.zu.ke.com/zufang')
response.encoding = 'utf-8'
response = requests.get('https://cd.zu.ke.com/zufang')
print(response.text)
response = requests.get('https://img2.baidu.com/it/u=1320841762,1779070643&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1680022800&t=f31623a18b2de3d02d91a0bbf5c57ced')
print(response.content)
response = requests.get('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js')
print(response.json())
2. 添加请求头
headers = {
'cookie': 'bid=3zJOvib4dZM; ap_v=0,6.0; _pk_ses.100001.4cf6=*; __gads=ID=fd8c89f20849e47a-22e45e97c0dc00eb:T=1679904293:RT=1679904293:S=ALNI_MZSmHQWH7--T48pczHe5sACveTTUg; __gpi=UID=00000be207bfa851:T=1679904293:RT=1679904293:S=ALNI_MamdCH7WtxSWoFKiiD2gza7FssGlw; _pk_id.100001.4cf6=8d8f89c6033fa666.1679904277.1.1679904312.1679904277.; __utma=30149280.1762753563.1679904312.1679904312.1679904312.1; __utmb=30149280.0.10.1679904312; __utmc=30149280; __utmz=30149280.1679904312.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.1733434124.1679904312.1679904312.1679904312.1; __utmb=223695111.0.10.1679904312; __utmc=223695111; __utmz=223695111.1679904312.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
'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('https://movie.douban.com/top250', headers=headers)
result = response.text
print(result)
3. 下载图片
response = requests.get('https://img2.baidu.com/it/u=1320841762,1779070643&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1680022800&t=f31623a18b2de3d02d91a0bbf5c57ced')
result = response.content
print(type(result))
with open(r'files\panda.jpg', 'wb') as f:
f.write(result)