python3 list列表随机选取一个

2023-01-31 00:01:15 列表 随机 选取

爬虫时适当更换user-agent可以稍微规避一下代理被封的风险。。。

from random import sample

ua = [
    '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) QQBrowser/6.9.11079.201',
    'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
    'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11',
    'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
    'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)',
    'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)']

print({
    'User-Agent': sample(ua, 1)[0]
})

 

相关文章