安装
pip install requests
如果time out
的话用:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
导入
import requests
基本操作
获取网站HTML
源码
import requests as res
url = 'https://example.com'
res1 = res.get(url)
print(res1.text)
下载文本文件
import requests as res
url = 'https://raw.githubusercontent.com/hexojs/hexo/master/README.md'
res1 = res.get(url)
print(res1.text)
获取的是Hexo
的README.md
下载二进制文件
音频文件和图片都是二进制文件
下载图片:URL
为https://testingcf.jsdelivr.net/gh/BlueSky01st/cdn@master/pic/emoji/valine_emoji/doge.png
import requests as res
url = 'https://testingcf.jsdelivr.net/gh/BlueSky01st/cdn@master/pic/emoji/valine_emoji/doge.png'
res1 = res.get(url)
pic = res1.content
with open('doge.png','wb') as f:
f.write(pic)
下载二进制文件时要用方法content
再写入文件