fake_useragent module not connecting properly - IndexError: list index out of range
fake_useragent module not connecting properly - IndexError: list index out of range
我尝试将 fake_useragent
模块与此块一起使用
from fake_useragent import UserAgent
ua = UserAgent()
print(ua.random)
但是当执行到这一行时ua = UserAgent()
,就会抛出这个错误
Traceback (most recent call last):
File "/home/hadi/Desktop/excel/gatewayform.py", line 191, in <module>
gate = GateWay()
File "/home/hadi/Desktop/excel/gatewayform.py", line 23, in __init__
ua = UserAgent()
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/fake.py", line 69, in __init__
self.load()
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/fake.py", line 75, in load
self.data = load_cached(
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 250, in load_cached
update(path, use_cache_server=use_cache_server, verify_ssl=verify_ssl)
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 245, in update
write(path, load(use_cache_server=use_cache_server, verify_ssl=verify_ssl))
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 178, in load
raise exc
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 154, in load
for item in get_browsers(verify_ssl=verify_ssl):
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 99, in get_browsers
html = html.split('<table class="w3-table-all notranslate">')[1]
IndexError: list index out of range
我使用 linux 并且我已经使用此命令安装了模块 pip3 install fake_useragent --upgrade
。
这个问题有什么解决办法吗?如果没有,有没有更好的模块可以使用?
Github pull request #110 对此有一个解决方案。基本上,您需要做的就是在 fake_useragent/utils.py
源代码的一行中更改一个字符。
要在您的系统上执行此操作,请使用管理员权限在您最喜欢的文本编辑器中打开 /usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py
†。转到第 99 行,并更改 w3
html = html.split('<table class="w3-table-all notranslate">')[1]
# ^^ change this
到ws
:
html = html.split('<table class="ws-table-all notranslate">')[1]
# ^^ to this
保存文件(具有管理员权限),重新启动您的 Python 会话,您的代码应该可以正常工作。
† 要找到 utils.py
所在的 fake_useragent
目录,运行 以下代码:
import fake_useragent
print(fake_useragent.__file__)
例如,在我的 Windows 笔记本电脑上,这打印了
'C:\Users\mattdmo\AppData\Roaming\Python\Python310\site-packages\fake_useragent\__init__.py'
因此要打开的文件夹是 C:\Users\mattdmo\AppData\Roaming\Python\Python310\site-packages\fake_useragent
。
我尝试将 fake_useragent
模块与此块一起使用
from fake_useragent import UserAgent
ua = UserAgent()
print(ua.random)
但是当执行到这一行时ua = UserAgent()
,就会抛出这个错误
Traceback (most recent call last):
File "/home/hadi/Desktop/excel/gatewayform.py", line 191, in <module>
gate = GateWay()
File "/home/hadi/Desktop/excel/gatewayform.py", line 23, in __init__
ua = UserAgent()
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/fake.py", line 69, in __init__
self.load()
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/fake.py", line 75, in load
self.data = load_cached(
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 250, in load_cached
update(path, use_cache_server=use_cache_server, verify_ssl=verify_ssl)
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 245, in update
write(path, load(use_cache_server=use_cache_server, verify_ssl=verify_ssl))
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 178, in load
raise exc
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 154, in load
for item in get_browsers(verify_ssl=verify_ssl):
File "/usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py", line 99, in get_browsers
html = html.split('<table class="w3-table-all notranslate">')[1]
IndexError: list index out of range
我使用 linux 并且我已经使用此命令安装了模块 pip3 install fake_useragent --upgrade
。
这个问题有什么解决办法吗?如果没有,有没有更好的模块可以使用?
Github pull request #110 对此有一个解决方案。基本上,您需要做的就是在 fake_useragent/utils.py
源代码的一行中更改一个字符。
要在您的系统上执行此操作,请使用管理员权限在您最喜欢的文本编辑器中打开 /usr/local/lib/python3.9/dist-packages/fake_useragent/utils.py
†。转到第 99 行,并更改 w3
html = html.split('<table class="w3-table-all notranslate">')[1]
# ^^ change this
到ws
:
html = html.split('<table class="ws-table-all notranslate">')[1]
# ^^ to this
保存文件(具有管理员权限),重新启动您的 Python 会话,您的代码应该可以正常工作。
† 要找到 utils.py
所在的 fake_useragent
目录,运行 以下代码:
import fake_useragent
print(fake_useragent.__file__)
例如,在我的 Windows 笔记本电脑上,这打印了
'C:\Users\mattdmo\AppData\Roaming\Python\Python310\site-packages\fake_useragent\__init__.py'
因此要打开的文件夹是 C:\Users\mattdmo\AppData\Roaming\Python\Python310\site-packages\fake_useragent
。