How to randomly get the user agent with the latest browser version? [duplicate]
You could do the following:
from fake_useragent import UserAgentimport randomimport redef 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])
The output of the script is:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194AOpera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/11.1Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Please note that the grp
function was blatantly stolen from this answer
If you instead are only looking for a single browser, like you asked, this would choose randomly while honoring the probabilities as lined out in the project's readme
browser = random.choice(ua.data_randomize)print(sorted(ua.data_browsers[browser], key=lambda a: grp(browsers[browser], a))[-1])