python爬虫之伪装User-Agent
程序员文章站
2022-05-09 15:01:58
...
因为爬虫的需要,所以需要多个user-agent,之前一直也是这样用的,后来接触到了fake_useragent这个包后,一切都变得那么简单简单了,fake_useragent是一个集成了市面上大部分的user-agent,可以指定浏览器,也可随机生成任意一个
这里简单做个生成指定浏览器的请求头
from fake_useragent import UserAgent
ua = UserAgent()
print(ua.ie)
print(ua.opera)
print(ua.chrome)
print(ua.google)
print(ua.firefox)
print(ua.safari)
print(ua.random)
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; chromeframe/13.0.782.215)
Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/5.0 Opera 11.11
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36
Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20130514 Firefox/21.0
Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1866.237 Safari/537.36
而在工作中常用的则是ua.random
我是这样来使用的
from fake_useragent import UserAgent
import requests
ua = UserAgent()
headers = {
'User-Agent': ua.random
}
url = 'https://www.baidu.com/'
response = requests.get(url, headers=headers)
print(response.status_code)
》》》 200