Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use random userAgent for Selenium (python)

Need use random userAgent. My code it is:

#!/usr/bin/python
# Import selenium
from selenium import webdriver

# init Profile options for navigation
fp = webdriver.FirefoxProfile()

# Set userAgent
fp.set_preference("general.useragent.override", "custom userAgent")
fp.update_preferences()
like image 860
Bryan Contreras Avatar asked Mar 06 '17 07:03

Bryan Contreras


People also ask

What is user-agent in selenium?

In short, User Agent is an identity of a client (user). User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions> Selenium does not implement any direct methods to read request or response headers.

How to fake user agent in Python requests?

We can fake the user agent by changing the User-Agent header of the request and bypass such User-Agent based blocking scripts used by websites. How to change User Agent To change the User-Agent using Python Requests, we can pass a dict with a key ‘User-Agent’ with the value as the User-Agent string of a real browser,

How to create a user-agent list in Python?

Put them in a Python List. Make each request pick a random string from this list and send the request with the ‘User-Agent’ header as this string. There are different methods to do it depending on the level of blocking you encounter. A user agent is a string that a browser or application sends to each website you visit.

How to get the user-agent from an instance of WebDriver?

Selenium does not have any direct method to query the user-agent from an instance of WebDriver. We need to use execute javascript in-built method to do this and pass the script that returns user-agent. Once the browser is started, we can get the user agent by executing following line of code.


Video Answer


1 Answers

I personally use the fake-useragent library.

You can generate random useragents with:

from fake_useragent import UserAgent
from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("general.useragent.override", UserAgent().random)
fp.update_preferences()
like image 139
crookedleaf Avatar answered Oct 19 '22 15:10

crookedleaf