# 构造多种请求头信息,并随机抽取信息
构造多个请求头信息
使用random随机抽取信息
话不多说,直接上代码
import random
# 构造请求头信息,随机抽取信息
agent1 = ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0‘
agent2 = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1‘
agent3 = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50‘
agent4 = ‘Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR ‘ \
‘3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0) ‘
agent5 = ‘Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR ‘ \
‘3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E) ‘
list1 = [agent1, agent2, agent3, agent4, agent5]
agent = random.choice(list1)
# 打印请求头信息
print(agent)
header = {
‘User-Agent‘: agent
}
具体使用请看https://blog.csdn.net/weixin_44752021/article/details/102600359
有什么不懂得或者您对这篇文章有什么指教的欢迎留言!
谢谢
原文地址:https://www.cnblogs.com/chx123/p/11707046.html