Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(fake_useragent) UserAgent() will not connect

Tags:

python

Essentially, I had a code that has been working for a few months. I try to run the program today and, like the title says, the connection for UserAgent()is timing out. I've tried upgrading the file with "pip install ---upgrade fake_useragent" and I'm told the package is up to date. I've also tried to delete the file (in order to re-install) but I am unable to for some reason. Does anyone have any ideas as to how else I can approach this issue?

from fake_useragent import UserAgent
...
ua = UserAgent()#program cannot progress past this point
like image 996
paulz Avatar asked Apr 28 '16 02:04

paulz


2 Answers

You should add a fallback user_agent to the ua object, this way if the server is down then the fallback useragent will kick in, better a working outdated u_agent than complete program crash.

from fake_useragent import UserAgent
ua = UserAgent(fallback='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36')
headers = {'User-Agent':ua.chrome}

I learned this from this question: Scrapy FakeUserAgentError: Error occurred during getting browser

like image 136
Diego Suarez Avatar answered Oct 18 '22 21:10

Diego Suarez


The fake_useragent package connects to the http://useragentstring.com/ to get the list of up-to-date user agent strings. Looks like the http://useragentstring.com/ is down and I hope it is temporarily.

like image 42
alecxe Avatar answered Oct 18 '22 22:10

alecxe