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

Python库fake-useragent

程序员文章站 2022-05-09 15:01:16
...

参考github链接https://github.com/hellysmile/fake-useragent
1.安装

pip install fake-useragent

2.使用案例
2.1查看各浏览器的user-agent值

from fake_useragent import UserAgent
ua = UserAgent()
#chrome浏览器
print(ua.chrome)
Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36

2.2查看随机值得重复

from fake_useragent import UserAgent

ua = UserAgent()
# ie浏览器的user agent
#print(ua.data_browsers)

print(ua.chrome)

for i in range(3):
    print(ua.random)

2.3爬虫应用

import requests
from fake_useragent import UserAgent
ua = UserAgent()
headers = {'User-Agent': ua.random}
url = 'http://待爬取的页面'
resp = requests.get(url, headers=headers)

 

相关标签: fake-useragent