(假用户代理)useragent()无法连接

2024-04-24 07:10:08 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我有一个代码已经运行了几个月。我今天试着运行这个程序,正如标题所说,UserAgent()的连接超时了。我尝试过用“pip install---upgrade fake_useragent”升级文件,但我被告知该软件包是最新的。我也试图删除文件(以便重新安装),但由于某些原因我无法删除。有人知道我还能怎么处理这个问题吗?在

from fake_useragent import UserAgent
...
ua = UserAgent()#program cannot progress past this point

Tags: installpip文件代码fromimport程序标题
2条回答

fake_useragent包连接到{a1},以获取最新的用户代理字符串列表。看起来http://useragentstring.com/坏了,我希望是暂时的。在

您应该向ua对象添加一个回退用户代理,这样如果服务器停机,那么回退用户代理就会启动,这比完全程序崩溃更好。在

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}

我从这个问题中学到了这一点: Scrapy FakeUserAgentError: Error occurred during getting browser

相关问题 更多 >