How to randomly get the user agent with the latest browser version? [duplicate]
This question already has an answer here:
Way to change Google Chrome user agent in Selenium?
2 answers
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?
python selenium selenium-webdriver web-scraping python-requests
marked as duplicate by DebanjanB
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 6:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Way to change Google Chrome user agent in Selenium?
2 answers
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?
python selenium selenium-webdriver web-scraping python-requests
marked as duplicate by DebanjanB
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 6:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You could edit the cached file directly
– Mad Physicist
Jan 1 at 23:45
Is it possible to remove the specific useragents by removing them from the list inua.data['browsers']
?
– Jarad
Jan 1 at 23:50
1
The marked duplicate is wrong, because it is a different question.
– feliks
Jan 2 at 13:18
add a comment |
This question already has an answer here:
Way to change Google Chrome user agent in Selenium?
2 answers
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?
python selenium selenium-webdriver web-scraping python-requests
This question already has an answer here:
Way to change Google Chrome user agent in Selenium?
2 answers
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?
This question already has an answer here:
Way to change Google Chrome user agent in Selenium?
2 answers
python selenium selenium-webdriver web-scraping python-requests
python selenium selenium-webdriver web-scraping python-requests
edited Jan 2 at 21:46
Mykola Zotko
asked Jan 1 at 23:12
Mykola ZotkoMykola Zotko
1,220315
1,220315
marked as duplicate by DebanjanB
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 6:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by DebanjanB
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 6:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You could edit the cached file directly
– Mad Physicist
Jan 1 at 23:45
Is it possible to remove the specific useragents by removing them from the list inua.data['browsers']
?
– Jarad
Jan 1 at 23:50
1
The marked duplicate is wrong, because it is a different question.
– feliks
Jan 2 at 13:18
add a comment |
You could edit the cached file directly
– Mad Physicist
Jan 1 at 23:45
Is it possible to remove the specific useragents by removing them from the list inua.data['browsers']
?
– Jarad
Jan 1 at 23:50
1
The marked duplicate is wrong, because it is a different question.
– feliks
Jan 2 at 13:18
You could edit the cached file directly
– Mad Physicist
Jan 1 at 23:45
You could edit the cached file directly
– Mad Physicist
Jan 1 at 23:45
Is it possible to remove the specific useragents by removing them from the list in
ua.data['browsers']
?– Jarad
Jan 1 at 23:50
Is it possible to remove the specific useragents by removing them from the list in
ua.data['browsers']
?– Jarad
Jan 1 at 23:50
1
1
The marked duplicate is wrong, because it is a different question.
– feliks
Jan 2 at 13:18
The marked duplicate is wrong, because it is a different question.
– feliks
Jan 2 at 13:18
add a comment |
1 Answer
1
active
oldest
votes
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'Operas.+$',
'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])
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 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 2 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.
– feliks
Jan 2 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 2 at 21:27
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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'Operas.+$',
'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])
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 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 2 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.
– feliks
Jan 2 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 2 at 21:27
|
show 2 more comments
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'Operas.+$',
'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])
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 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 2 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.
– feliks
Jan 2 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 2 at 21:27
|
show 2 more comments
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'Operas.+$',
'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])
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'Operas.+$',
'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])
edited Jan 2 at 12:42
KbiR
2,38611437
2,38611437
answered Jan 2 at 0:14
feliksfeliks
967115
967115
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 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 2 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.
– feliks
Jan 2 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 2 at 21:27
|
show 2 more comments
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 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 2 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.
– feliks
Jan 2 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 2 at 21:27
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
This method generates only 5 user agents. Is it possible to generate more?
– Mykola Zotko
Jan 2 at 11:21
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 at 13:15
You can wrap the last two lines of my post in a function and call it multiple times.
– feliks
Jan 2 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 2 at 14:04
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 2 at 14:04
1
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.
– feliks
Jan 2 at 16:17
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.
– feliks
Jan 2 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 2 at 21:27
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 2 at 21:27
|
show 2 more comments
You could edit the cached file directly
– Mad Physicist
Jan 1 at 23:45
Is it possible to remove the specific useragents by removing them from the list in
ua.data['browsers']
?– Jarad
Jan 1 at 23:50
1
The marked duplicate is wrong, because it is a different question.
– feliks
Jan 2 at 13:18