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()
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.
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,
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With