Fake-useragent is an open source project for crawlers. You don’t need to build your own UA pool, you just need to call the corresponding method. It’s easy to download and try.
Method of use
from fake_useragent import UserAgent
ua = UserAgent()
ua.ie
# Mozilla / 5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
ua.msie
# Mozilla / 5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident / 6.0) '
ua['Internet Explorer']
# Mozilla / 5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident / 4.0; GTB7.4; InfoPath.2; SV1; The.net CLR 3.3.69573; WOW64; en-US)
ua.opera
# Opera/(9.80 X11; Linux i686; U; Ru) Presto / 2.8.131 Version / 11.11
ua.chrome
# Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2
ua.google
# Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13
ua['google chrome']
# Mozilla / 5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
ua.firefox
# Mozilla/5.0 (Windows NT 6.2; Win64; x64; The rv: Gecko / 20121011 Firefox / 16.0.1 16.0.1)
ua.ff
# Mozilla / 5.0 (X11; Ubuntu; Linux i686; The rv: 15.0) Gecko / 20100101 Firefox / 15.0.1
ua.safari
# Mozilla / 5.0 (the device; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
# and the best one, random via real world browser usage statistic
ua.random
# Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Copy the code
notes
Fake-useragent stores the collected data in a temporary directory of your operating system, such as/TMP
If you only want to update the saved database:
from fake_useragent import UserAgent
ua = UserAgent()
ua.update()
Copy the code
If you don’t want to cache the database or don’t have a writable file system:
from fake_useragent import UserAgent
ua = UserAgent(cache=False)
Copy the code
Sometimes, UserAgentString.com or w3schools.com changes their HTML or changes it downward, in which case fax-UserAgent uses Heroku backup
If you don’t want to use a managed cache server (version 0.1.5 added)
from fake_useragent import UserAgent
ua = UserAgent(use_cache_server=False)
Copy the code
In rare cases, fake-useragent will not be able to download data if the hosted cache server and source are not available :(version 0.1.3 added)
from fake_useragent import UserAgent
ua = UserAgent()
# Traceback (most recent call last):
#...
# fake_useragent.errors.FakeUserAgentError
# You can catch it via
from fake_useragent import FakeUserAgentError
try:
ua = UserAgent()
except FakeUserAgentError:
pass
Copy the code
If you try to get an unknown browser :(version 0.1.3 has changed)
from fake_useragent import UserAgent
ua = UserAgent()
ua.best_browser
# Traceback (most recent call last):
#...
# fake_useragent.errors.FakeUserAgentError
Copy the code
You can completely disable any annoying exceptions fallback by adding :(version 0.1.4 added)
import fake_useragent
ua = fake_useragent.UserAgent(fallback='Your favorite Browser')
# in case if something went wrong, one more time it is REALLY!!! rare case
ua.random == 'Your favorite Browser'
Copy the code
Do you want to control the location of data files? (Version 0.1.4 added)
import fake_useragent
# I am STRONGLY!!! recommend to use version suffix
location = '/home/user/fake_useragent%s.json' % fake_useragent.VERSION
ua = fake_useragent.UserAgent(path=location)
ua.randoms
Copy the code
If you need to protect some attributes from __getattr__ using the method to override some attributes in the UserAgent, you can pass the attribute name here at safe_attrs. At the very least, this will prevent you from raising FakeUserAgentError if the attribute is not found.
For example, to use fake_userAgent with injection, you need:
import fake_useragent
ua = fake_useragent.UserAgent(safe_attrs=('__injections__')),Copy the code
Please do not use it if you do not understand why you need it. This is inconceivable in rare cases.
Encounter problems??
Make sure you are using the latest version!
pip install -U fake-useragent
Copy the code
Check the version from the python console :(version 0.1.4 added)
import fake_useragent
print(fake_useragent.VERSION)
Copy the code
The installation
pip install fake-useragent
Copy the code
Open source address:Github.com/hellysmile/…
Do you like today’s recommendation? If you like the words, please leave a message at the bottom of the article or like, to express my support, your message, like, forward attention is I continue to update the power oh!
Concern public number reply: “1024”, free to receive a large wave of learning resources, first come, first served oh!