Requests的安装:
pip install Requests
Requests的使用:
import requests url = "http://www.mzitu.com" response = requests.get(url) # 获得请求 response.encoding = "utf-8" # 改变其编码 html = response.text # 获得网页内容 binary__content = response.content # 获得二进制数据 raw = requests.get(url, stream=True) # 获得原始响应内容 headers = {‘user-agent‘: ‘my-app/0.0.1‘} # 定制请求头 r = requests.get(url, headers=headers) cookies = {"cookie": "# your cookie"} # cookie的使用 r = requests.get(url, cookies=cookies) # 后续
tips:
raw = requests.get(url, stream=True) # 将文本流保存到文件 filename = ‘example.jpg‘ with open(filename, ‘wb‘) as fd: for chunk in raw.iter_content(): fd.write(chunk) fd.close()
时间: 2024-10-29 10:46:37