文档地址:http://docs.python-requests.org/zh_CN/latest/index.html
import requests
res = requests.get('https://www.baidu.com/')
# print(type(res.text))
# print(res.text)
# print(type(res.content))
# print(res.content.decode('utf-8')) # 获取网页源码
# print(res.url) # 请求的url
# print(res.encoding) # 查看响应头部字符编码
# print(res.status_code) # 响应状态码
"""
百度浏览器 查看中国两个字
"""
param = {
'wd': '中国',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/72.0.3626.121 Safari/537.36'
}
url = 'https://www.baidu.com/s'
res = requests.get(url=url, headers=headers, params=param)
with open('/Users/mac/PycharmProjects/TEST/TEST/爬虫day/file/requests.html', 'w', encoding='utf-8') as f:
f.write(res.content.decode('utf-8'))
"""
requests 笔记
1,response.text这个是requests,将response.content进行解码的字符串。解码需要指定一个编码方式,request会根据自己的猜测来判断编码的方式。
所以有时候可能会猜测错误,就会导致解码产生乱码,这个时候就应该使用response.content.decode('utf-8)进行手动解码
response.content:这个是直接从网络上面抓取的数据,没有经过任何解码,所以是一个bytes类型。其实在硬盘上和在网络上传输的字符串都是bytes类型
"""