IndexError: list index out of range

原帖:fake-useragent库没正确发挥作用出现索引超出范围问题


代码重现

from fake_useragent import UserAgent
import random

ua = UserAgent(path="")    # 如果下载在默认位置,无须强调路径。
headers = {'User-Agent': ua.random}

报错重现

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

关键部分(辨识):ua = UserAgent() 后进入fake.py utils.py......报错html = ......至IndexError。

解决方案

①From Github pull request #110. 就是找到fake_useragent/utils.py source code. 然后Go to line 99, change the w3 to ws:

html = html.split('<table class="w3-table-all notranslate">')[1]

↓↓↓

html = html.split('<table class="ws-table-all notranslate">')[1]

②更新版本


原因分析暂时没深究,可看看原帖提供的github是否有说 为啥米


时隔N月后,我回来了。首先祝福大家兔年吉祥,前途似锦,身体健康!

忙于秋招,如今闲下来,重拾爱好。祝好,望列位不负热爱!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值