6

fake_useragent package can randomly generate user agents:

from fake_useragent import UserAgent

ua = UserAgent()
user_agent = ua.random

Sometimes generated user agents have outdated browser versions and some websites don‘t accept them. Is there any way to generate user agents only with the latest browser versions?

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73

1 Answers1

4

You could do the following:

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])

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.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

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])
KbiR
  • 4,047
  • 6
  • 37
  • 103
Felix
  • 1,837
  • 9
  • 26
  • This method generates only 5 user agents. Is it possible to generate more? – Mykola Zotko Jan 02 '19 at 11:21
  • You can wrap the last two lines of my post in a function and call it multiple times. – Felix Jan 02 '19 at 13:15
  • I ask about unique user agents. Your script will alwas generate one of those 5 unique user agents, which you posted above, and it doesn‘t matter how many times you call a function with it. – Mykola Zotko Jan 02 '19 at 14:04
  • 1
    You asked "Is there any way to generate user agents only with the latest browser versions?". Of course this leads to the latest browser version for each respective browser. I hope this makes it clear why there is no way that multiple browser versions are the latest version, because only one single version can be the latest version. Maybe you should rephrase your question, if you want to know something different. – Felix Jan 02 '19 at 16:17
  • I thought it should be more. You can run for example chrome, safari and firefox on windows, mac and linux and you get 9 user agents at least. But It looks like fake_useragent module has a limited amount of user agents and the most of them have outdated browser versions. – Mykola Zotko Jan 02 '19 at 21:27
  • Why should it be more? As explained in the projects readme, the site useragentstring.com is used to get the user agents. That database does not seem to save the date of the user agents transmission, which would help selecting older user agents. You could easily select the last 3 user agents by changing `[-1]` to `[-3:]` in the last line I gave. – Felix Jan 02 '19 at 21:37
  • It looks like the database of useragentstring.com is out of date, because your 5 latest user_agents are outdated as well: `Chrome/41.0.2228.0` version 41 vs actual version 71; `AppleWebKit/537.75.14` version 7 vs 12; `Opera/9.80` version 9.8 vs 11.1; `Firefox/40.1` version 40 vs 60; `Trident/7.0` version 7 vs 11. – Mykola Zotko Jan 02 '19 at 22:08