如何随机获得具有最新浏览器版本的用户代理?

时间:2019-01-01 23:12:25

标签: python selenium selenium-webdriver web-scraping python-requests

n'软件包可以随机生成用户代理:

Theorem evenb_double_k' : forall n,
  evenb n = true -> exists k, n = double k.
Proof.
  apply even_ind.
  - exists 0. reflexivity.
  - intros n H [k Hk].
    exists (S k).
    rewrite Hk.
    reflexivity.
Qed.

有时生成的用户代理的浏览器版本已过时,某些网站不接受它们。有什么方法只能使用最新的浏览器版本生成用户代理?

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

from fake_useragent import UserAgent
import random
import re

def grp(pat, txt):
    r = re.search(pat, txt)
    return r.group(0) if r else '&'

ua = UserAgent()
browsers = {
    'chrome': r'Chrome/[^ ]+',
    'safari': r'AppleWebKit/[^ ]+',
    'opera': r'Opera\s.+$',
    'firefox': r'Firefox/.+$',
    'internetexplorer': r'Trident/[^;]+',
}

for k, v in browsers.items():
    print(sorted(ua.data_browsers[k], key=lambda a: grp(v, a))[-1])

脚本的输出为:

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A
Opera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/11.1
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1
Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0;  rv:11.0) like Gecko

请注意,grp函数是从this answer偷来的

如果您只想查找单个浏览器(如您所问的那样),则它将随机选择,同时遵守project's readme

中列出的概率
browser = random.choice(ua.data_randomize)
print(sorted(ua.data_browsers[browser], key=lambda a: grp(browsers[browser], a))[-1])