在进行漂亮的汤解析的for循环期间,为什么会出现“ IndexError:列表索引超出范围”?

prime90

我正在尝试从82个URL解析图像源,这些URL存储在site_links以beautifulsoup命名的列表中我不知道为什么这个循环在中途抛出错误。有什么想法吗?

错误:

/images/africa/egypt/abu-gorab-sun-temples/sun-temple-of-niuserre-main.jpg
/images/africa/egypt/abu-roash-pyramid-of-djedefre/abu-roash-pyramid-of-djedefre-main.jpg
/images/africa/egypt/abusir-necropolis/abusir-necropolis-main1.jpg
/images/africa/egypt/dashur-bent-pyramid/dashur-bent-pyramid-main1.jpg
/images/africa/egypt/giza-plateau-pyramid-complex/giza-plateau-pyramid-complex-main1.jpg
/images/africa/egypt/giza-plateau-sphinx/giza-plateau-sphinx-main1.jpg
/images/africa/egypt/zawyet-el-aryan-unfinished-pyramid/zawyet-el-aryan-unfinished-pyramid-main2.jpg
/images/africa/egypt/abu-simbel-temple-complex/abu-simbel-temple-complex-main1.jpg
/images/africa/egypt/aswan-elephantine-island/aswan-elephantine-island-main.jpg
/images/africa/egypt/denderra-temple-complex/denderra-temple-complex-main2.jpg
/images/africa/egypt/thebes-karnak-temple-complex/thebes-karnak-temple-complex-main5.jpg
/images/africa/egypt/thebes-luxor-temple/thebes-luxor-temple-main3.jpg
/images/africa/ethiopia/axum-obelisks/axum-obelisks-main1.jpg
/images/africa/ethiopia/lalibela-rock-hewn-churches/lalibela-rock-hewn-churches-main3.jpg
/images/asia/india/ellora-kailasa-temple/ellora-kailasa-temple-main1.jpg
/images/asia/india/warangal-warangal-fort/warangal-warangal-fort-main1.jpg
/images/asia/indonesia/west-java-gunung-padang/west-java-gunung-padang-main1.jpg
/images/asia/japan/yonaguni-yonaguni-monument/yonaguni-yonaguni-monument-main1.jpg
/images/asia/laos/xiangkhouang-plain-of-jars/xiangkhouang-plain-of-jars-main1.jpg
/images/asia/lebanon/baalbek-baalbek-temple-complex/baalbek-baalbek-temple-complex-main4.jpg
/images/asia/micronesia/pohnpei-nan-madol/pohnpei-nan-madol-main1.jpg
Traceback (most recent call last):
  File "c:/Users/J/Google Drive/pythonProjects/Megalith Map/data_scrape.py", line 41, in <module>
    img = soup.find('div', {'itemprop' : 'blogPost'}).find_all('img')[0].get('src')
IndexError: list index out of range

我的代码:

site_links = []
site_img = []
# PARSES ALL IMAGE SOURCES ON THE WEBSITE
for i in site_links:
        r = requests.get(i).text
        soup = bs4.BeautifulSoup(r, 'html5lib')
        img = soup.find('div', {'itemprop' : 'blogPost'}).find_all('img')[0].get('src')
        if '.jpg' in img:
                site_img.append(site_img)
                print(img)
gmds

的结果find_all是类似list如果尝试在它为空时对其编制索引,则会引发错误。

这表明它在页面上找不到符合您指定条件的任何内容。要处理这种情况,您应该首先检查是否find_all找到了任何东西,然后再对其进行索引:

site_links = []
site_img = []
# PARSES ALL IMAGE SOURCES ON THE WEBSITE
for i in site_links:
    r = requests.get(i).text
    soup = bs4.BeautifulSoup(r, 'html5lib')
    images = soup.find('div', {'itemprop' : 'blogPost'}).find_all('img')
    if images:
        img = images[0].get('src', '')
        if '.jpg' in img:
            site_img.append(site_img)
            print(img)

    else:
        print('No image found.')

请注意,我还修改了get如果src找不到则返回空字符串调用,这也将防止引发错误,因为否则将返回错误None,这将导致以下包含测试失败。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我得到“IndexError:列表索引超出范围”

Python 列表列表为什么会出现此错误:IndexError:列表索引超出范围?

While循环-IndexError:列表分配索引超出范围

while循环-IndexError:列表索引超出范围

为什么为什么不断出现错误“ IndexError:列表索引超出范围”?

为什么此迭代列表增长代码使IndexError:列表分配索引超出范围?

为什么会出现错误:列表索引超出范围

IndexError:列表索引超出范围:即使在列表未包含之后,也会出错

将循环转换为列表推导以获取IndexError:列表索引超出范围

为什么会收到此错误?IndexError:列表索引超出范围

IndexError:列表索引超出范围:我可以理解为什么会发生此错误

当我运行代码时,为什么显示IndexError:列表索引超出范围?

谁能告诉我为什么我得到IndexError:列表索引超出范围?

美汤,列表索引超出范围

运行节俭示例时出现“ IndexError:列表索引超出范围”错误

尝试使用keras的vgg16加载权重时出现“ IndexError:列表索引超出范围”

Python代码昨天运行良好,现在出现此“ IndexError:列表索引超出范围”

Flask / Pymongo / Restplus-使用update(** data)时,出现“ IndexError:列表索引超出范围”

如果出现错误“ IndexError:列表索引超出范围”,请重新加载页面

IndexError:将值从for循环分配给元组时,列表索引超出范围

(IndexError:列表索引超出范围)我的for循环给我一个错误?

使用简单输入进行拓扑排序会导致“ IndexError:列表索引超出范围”

出现“列表索引超出范围”错误

列表索引超出范围与条件循环

For循环错误:列表索引超出范围

嵌套for循环的列表索引超出范围

IndexError:在 readlines() 循环中列出超出范围的索引

IndexError:通过for循环列出索引超出范围的错误

IndexError:解析方法参数时,元组索引超出范围