我试着使用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
。
有解决此问题的解决方案吗?如果没有,是否有更好的模块可用?
html = html.split('<table class="w3-table-all notranslate">')[1]
而不是fake_useragent
。 - badukerfake_useragent
模块中的utils.py
文件。 - Hadi Ayoub