TypeError:列表索引必须是整数,而不是str Python E

2024-05-15 04:13:26 发布

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

所以我在编写一个脚本来扫描url并获取响应代码时收到了这个错误,然后我收到了这个错误。我在堆栈溢出、Google等搜索过很多地方,所有这个错误的答案都与这个情况无关。这可以让陷入同样情况的用户受益。在

for dirs in dds_dirpath:
        if responsecode(dds_host + dds_dirpath[dirs]) == "200" or responsecode(dds_host + dds_dirpath[dirs]) == "201" or responsecode(dds_host + dds_dirpath[dirs]) == "202" or responsecode(dds_host + dds_dirpath[dirs]) == "203" or responsecode(dds_host + dds_dirpath[dirs]) == "204" or responsecode(dds_host + dds_dirpath[dirs]) == "205" or responsecode(dds_host + dds_dirpath[dirs]) == "206" or responsecode(dds_host + dds_dirpath[dirs]) == "207" or responsecode(dds_host + dds_dirpath[dirs]) == "208" or responsecode(dds_host + dds_dirpath[dirs]) == "226" or responsecode(dds_host + dds_dirpath[dirs]) == "401":
            print "[   \033[1;32;40mOK\033[1;37;40m   ] ", dds_host + dds_dirpath[dirs]
        else:
            print "[ FAILED ] ", dds_host + dds_dirpath[dirs]

谢谢,谢谢你的帮助。我不认为这是我的问题,因为我在同样的情况下发现了未回答的问题。在


Tags: or代码脚本hosturl堆栈地方错误
1条回答
网友
1楼 · 发布于 2024-05-15 04:13:26

要解决您的问题,只需将dds_dirpath[dirs]替换为dirs。dds_dirpath[dirs]没有任何意义,因为dirs变量inself将迭代dds_dirpath,并将获取,而不是该列表中的索引

for dirs in dds_dirpath: 
    if responsecode(dds_host + dirs) == "200" or responsecode(dds_host + dirs) == "201" or responsecode(dds_host + dirs) == "202" or responsecode(dds_host + dirs) == "203" or responsecode(dds_host + dirs) == "204" or responsecode(dds_host + dirs) == "205" or responsecode(dds_host + dirs) == "206" or responsecode(dds_host + dirs) == "207" or responsecode(dds_host + dirs) == "208" or responsecode(dds_host + dirs) == "226" or responsecode(dds_host + dirs) == "401": 
        print "[ \033[1;32;40mOK\033[1;37;40m ] ", dds_host + dirs 
    else: 
        print "[ FAILED ] ", dds_host + dirs

我不能忽视你11次请求网址的事实(!)在if条件下。这是不好的,因为网站管理员不喜欢来自同一IP的频繁请求,并可能会禁止您的IP

你应该这样做

^{pr2}$

你真的是以字符串而不是int来获取响应代码吗?在

相关问题 更多 >

    热门问题