欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fake_useragent伪装headers

程序员文章站 2022-05-09 11:18:19
...

关于反爬的问题,我们要经常伪装不同的浏览器User-Agent来欺骗服务器来完成请求,fake_useragent库则可以模拟指定浏览器的User-Agent或者随机产生一个User-Agent。

  • 安装
pip install fake_useragent
  • 指定浏览器
from fake_useragent  import UserAgent
ua = UserAgent()

# chrome
print(ua.chrome)
# ie
print(ua.ie)
# firefox
print(ua.firefox)
# opera
print(ua.opera)
# safari
print(ua.safari)

返回虚拟 User-Agent

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; InfoPath.2)
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1
Opera/9.80 (Windows NT 6.1; U; en-GB) Presto/2.7.62 Version/11.00
Mozilla/5.0 (Windows; U; Windows NT 6.0; fr-FR) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5
  • 随机浏览器
from fake_useragent  import UserAgent
ua = UserAgent()
print(ua.random)

# 结果
Mozilla/5.0 (X11; OpenBSD i386) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
  • 案例说明
import requests
from fake_useragent  import UserAgent
ua = UserAgent()
headers = {'User-Agent': ua.random}
url = 'http://www.baidu.com'
req = requests.get(url,headers=headers)
print(req.status_code)
import requests
from fake_useragent import UserAgent

def fakeUserAgent():
    ua = UserAgent()
    headers = {'User-Agent': ua.random}
    return headers

if __name__ == '__main__':
    url = 'http://www.baidu.com'
    req = requests.get(url, headers=fakeUserAgent())
    print(req.status_code)