吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1078|回复: 13
收起左侧

[求助] python 'list index out of range'?

[复制链接]
double07 发表于 2021-4-29 18:16
本帖最后由 double07 于 2021-4-29 19:01 编辑

[Python] 纯文本查看 复制代码
<td class="rowfollow nowrap" valign="middle" style='padding: 0'><a href="?cat=419"><img src="pic/cattrans.gif" alt="Movie(電影)/HD" title="Movie(電影)/HD" style="background-image: url(pic/category/chd/scenetorrents/cht/moviehd.png);" /></a></td>
<td class="torrenttr" width="100%" align="left"><table class="torrentname" width="100%"><tr class="sticky_top"><td class="torrentimg"><a title="To Be or Not to Be 1942.1080p FLAC x264-Geek" href="details.php?id=479986&hit=1"><img alt="torrent thumbnail" src="https://img9.doubanio.com/view/photo/l_ratio_poster/public/p1339915772.jpg"></a></td><td class="embedded"><img class="sticky" src="pic/trans.gif" alt="Sticky" title="一級置頂" /><img class="sticky" src="pic/trans.gif" alt="Sticky" title="一級置頂" />  <img src="pic/ms_up.gif" align="absmiddle" title="+20%" alt="+20%"> <a title="To Be or Not to Be 1942.1080p FLAC x264-Geek" href="details.php?id=479986&hit=1"><b>To Be or Not to Be 1942.1080p FLAC x264-Geek</b></a> <img class="pro_50pctdown" src="pic/trans.gif" alt="50%" title="50%" /> <img src="pic/trans.gif" class="label_sub" alt="中字" /><br />你逃我也逃 (1942) [Geek预热作品]</td><td width="80" class="embedded" style="text-align: right; " valign="middle"><a href="download.php?id=479986&https=1"><img class="download" src="pic/trans.gif" style='padding-bottom: 2px;' alt="download" title="下載本種" /></a><a id="bookmark0"  href="javascript: bookmark(479986,0);" ><img class="delbookmark" src="pic/trans.gif" alt="Unbookmarked" title="收藏" /></a><br /><img src="/pic/imdb.gif" alt="IMDB:" /> <a >8.3</a></td>
</tr></table></td><td class="rowfollow"><b><a href="details.php?id=479986&hit=1&cmtpage=1#startcomments" >11</a></b></td><td class="rowfollow nowrap"><span title="2021-04-18 21:44:44">10天<br />20時</span></td><td class="rowfollow">13.59<br />GB</td><td class="rowfollow" align="center"><b><a href="details.php?id=479986&hit=1&dllist=1#seeders">151</a></b></td>
<td class="rowfollow"><b><a href="details.php?id=479986&hit=1&dllist=1#leechers">1</a></b></td>
<td class="rowfollow"><a href="viewsnatches.php?id=479986"><b>274</b></a></td>
<td class="rowfollow" style="font-weight: bold">--</td><td class="rowfollow"><i>匿名</i></td>


用pycharm debug调试能得到种子数(见图)
微信图片_20210429181037.png
但正式运行却这样提示?问题出在哪里?
微信图片_20210429181044.png

这应该是问题所在:通过循环对结果一一检查,发现,当“种子数”较少时,会在<a>标签间增加一段颜色代码:<a href="details.php?id=438306&amp;hit=1&amp;dllist=1#seeders"><font color="#ff0000">1</font></a>导致的吧,但怎样解决?

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

fanvalen 发表于 2021-4-29 18:24
你那不是list列表,{}是字典是dic
你用list方法去操作字典是不行的
list=[]这才是列表
Test_dada 发表于 2021-4-29 18:27
这是提示超出索引了,52行的代码  i.xpath(...)[0]  这句打印下看看有没有值
Test_dada 发表于 2021-4-29 18:30
fanvalen 发表于 2021-4-29 18:24
你那不是list列表,{}是字典是dic
你用list方法去操作字典是不行的
list=[]这才是列表

他命名是list,但代码是list[‘’键名‘’]=键值,是字典的赋值方法
 楼主| double07 发表于 2021-4-29 18:35
本帖最后由 double07 于 2021-4-29 18:38 编辑
Test_dada 发表于 2021-4-29 18:27
这是提示超出索引了,52行的代码  i.xpath(...)[0]  这句打印下看看有没有值

有值,148。同时测试了len,显示1
image.png
fanvalen 发表于 2021-4-29 18:52
在xpath之前先print(i)来看看是什么
 楼主| double07 发表于 2021-4-29 18:59
一个个循环结果检查,发现,当“种子数”较少时,会在<a>标签间增加一段颜色代码:<a href="details.php?id=438306&amp;hit=1&amp;dllist=1#seeders"><font color="#ff0000">1</font></a>导致的吧,但怎样解决?
Ercilan 发表于 2021-4-29 19:31
分两种情况筛选测试呗。第一种不行再尝试第二种
metaxman 发表于 2021-4-29 19:54
本帖最后由 metaxman 于 2021-4-29 19:56 编辑

Test_dada 发表于 2021-4-29 18:30
他命名是list,但代码是list[‘’键名‘’]=键值,是字典的赋值方法

这代码变量名也是没谁了?list都拿来直接用来做变量名
后面如果用到list( ),岂不尴尬 and 手足无措

Test_dada 发表于 2021-4-30 09:19
本帖最后由 Test_dada 于 2021-4-30 09:24 编辑
double07 发表于 2021-4-29 18:59
一个个循环结果检查,发现,当“种子数”较少时,会在标签间增加一段颜色代码:1导致的吧,但怎样解决?

<a href="details.php?id=438306&hit=1&dllist=1#seeders">
<font color="#ff0000">1</font>
</a>
这里的时候,明显a标签text()拿不到数据,文本在font里面
这里可以加个try,判断异常的时候就取font的值
try:
      list['...'] = i.xpath(...)[0]...
except Exception as e:
      print(str(e),"代码异常,尝试font.text()")
      
list['...'] = i.xpath(...)[0]...  # 这里写的是<font color="#ff0000">1</font>的文本取值

免费评分

参与人数 1热心值 +1 收起 理由
double07 + 1 学习了

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 13:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表