浏览器检测网站
https://ie.icoa.cn/
http://useragentstring.com/index.php
通过headers中设置参数来伪装浏览器
# 模拟UA
import requests
from lxml import etree
# 获取内容
url="https://ie.icoa.cn/"
# 伪装头部
head={'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
res=requests.get(url,headers=head)
res.encoding='utf-8'
root=etree.HTML(res.text)
ua=root.xpath('//table[@class="zebra"]/tr/td/i/text()') # 获取UA内容
print(ua)
image.png
通过快速工具fake-useragent来快速伪装常见浏览器
安装:pip install fake-useragent
官网文档:https://pypi.org/project/fake-useragent/
from fake_useragent import UserAgent
ua=UserAgent()
# 随机产生一个UA
print(ua.random)
# 产生指定UA
print(ua.ie) # IE UA
print(ua.chrome) # Chrome UA
print(ua.firefox) # Firefox UA
# 随机产生多个UA
for i in range(5):
print(ua.random)
# 浏览器代理池
import requests
from lxml import etree
import random
from fake_useragent import UserAgent
ua=UserAgent()
uas=[]
for i in range(5):
uas.append(ua.random)
print(uas)
for i in range(5):
this_ua=random.choice(uas)
url="http://ie.icoa.cn/"
head={"user-agent":this_ua}
res=requests.get(url,headers=head)
res.encoding='utf-8'
root=etree.HTML(res.text)
ua_rst=root.xpath('//table[@class="zebra"]/tr/td/i/text()') # 获取UA内容
print(ua_rst)
image.png
image.png
网友评论