如果不改变header,往往只能获取到很少一部分的content。所以我们要改变header
import requests
respones = requests.get("https://www.baidu.com")
print(respones.status_code)
print(respones.headers)
print(respones.request.headers)
print(respones.content.decode())
print("更改headers后")
headers1 = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) App1eWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36"}
respones1 = requests.get("https://www.baidu.com",headers = headers1)
print(respones1.content.decode())
带参数
import requests
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}
p = {"wd":"传智播客"}
url_temp = "https://www.baidu.com/s?"
r = requests.get(url_temp,headers=headers,params=p)
print(r.status_code)
print(r.request.url)
也可以这样
import requests
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}
url = "https://www.baidu.com/s?wd={}".format("传智播客")
r = requests.get(url,headers=headers)
print(r.status_code)
print(r.request.url)
运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/b06cc34519fa9a876b46bdb13af6b0f5.png)