尝试并除外不与IndexError一起使用

时间:2019-05-18 21:08:24

标签: python parsing for-loop web-scraping beautifulsoup

我正在尝试使用beautifulsoup从大约82个URL上的两个位置解析图像。位置是img_pageimg_gallery。这两个位置之一应具有我要解析的图像。该代码一直有效,直到两个位置都没有图像,并且我将收到以下错误:

***GALLERY*** /images/africa/egypt/abu-gorab-sun-temples/gallery/sun-temple-of-niuserre-2.jpg
***GALLERY*** /images/africa/egypt/abu-roash-pyramid-of-djedefre/gallery/abu-roash-pyramid-of-djedefre-10.jpg
***GALLERY*** /images/africa/egypt/abusir-necropolis/gallery/abusir-necropolis-01.jpg
***GALLERY*** /images/africa/egypt/dashur-bent-pyramid/gallery/dashur-bent-pyramid-01.jpg
***GALLERY*** /images/africa/egypt/giza-plateau-pyramid-complex/gallery/giza-plateau-pyramid-complex-01.jpg
***GALLERY*** /images/africa/egypt/giza-plateau-sphinx/gallery/giza-plateau-sphinx-01.jpg
***GALLERY*** /images/africa/egypt/giza-plateau-valley-temple/gallery/giza-plateau-valley-temple-01.jpg
***GALLERY*** /images/africa/egypt/saqqara-serapeum/gallery/saqqara-serapeum-01.jpg
***GALLERY*** /images/africa/egypt/zawyet-el-aryan-unfinished-pyramid/gallery/zawyet-el-aryan-unfinished-pyramid-01.jpg
***GALLERY*** /images/africa/egypt/abu-simbel-temple-complex/gallery/abu-simbel-temple-complex-01.jpg
***GALLERY*** /images/africa/egypt/abydos-osireion/gallery/osireion-abydos-01.jpg
**PAGE*** /images/africa/egypt/aswan-elephantine-island/aswan-elephantine-island-main.jpg
***GALLERY*** /images/africa/egypt/aswan-unfinished-obelisk/gallery/aswan-unfinished-obelisk-01.jpg
***GALLERY*** /images/africa/egypt/denderra-temple-complex/gallery/denderra-temple-complex-01.jpg
***GALLERY*** /images/africa/egypt/thebes-karnak-temple-complex/gallery/thebes-karnak-temple-complex-01.jpg
***GALLERY*** /images/africa/egypt/thebes-luxor-temple/gallery/thebes-luxor-temple-01.jpg
***GALLERY*** /images/africa/ethiopia/axum-obelisks/gallery/axum-obelisks-01.jpg
**PAGE*** /images/africa/ethiopia/lalibela-rock-hewn-churches/lalibela-rock-hewn-churches-main3.jpg
***GALLERY*** /images/asia/india/ellora-kailasa-temple/gallery/ellora-kailasa-temple-01.jpg
***GALLERY*** /images/asia/india/warangal-warangal-fort/gallery/warangal-warangal-fort-01.jpg
***GALLERY*** /images/asia/indonesia/west-java-gunung-padang/gallery/west-java-gunung-padang-01.jpg
***GALLERY*** /images/asia/israel/jerusalem-western-wall/gallery/jerusalem-western-wall01.jpg
***GALLERY*** /images/asia/japan/asuka-ishibutai-kofun/gallery/asuka-ishibutai-kofun-01.jpg
***GALLERY*** /images/asia/japan/asuka-masuda-no-iwafune/gallery/asuka-masuda-no-iwafune-01.jpg
**PAGE*** /images/asia/japan/yonaguni-yonaguni-monument/yonaguni-yonaguni-monument-main1.jpg
***GALLERY*** /images/asia/laos/xiangkhouang-plain-of-jars/gallery/xiangkhouang-plain-of-jars-01.jpg
***GALLERY*** /images/asia/lebanon/baalbek-baalbek-temple-complex/gallery/baalbek-baalbek-temple-complex-01.jpg
**PAGE*** /images/asia/micronesia/pohnpei-nan-madol/pohnpei-nan-madol-main1.jpg
***GALLERY*** /images/asia/south-korea/ganghwa-ganghwa-dolmen-site/gallery/ganghwa-ganghwa-dolmen-site-01.jpg
Traceback (most recent call last):
  File "c:/Users/JGrov/Google Drive/pythonProjects/Megalith Map/image.py", line 42, in <module>
    img_src = img_page[0].get('src', '')
IndexError: list index out of range

然后,我认为我可以使用tryexcept来保持脚本运行并将'No Images Found'附加到site_img上,但是我似乎无法使其正常工作。有什么建议么?谢谢!

我当前的代码:

site_link = []
site_img = []
for i in site_links:
    r = requests.get(i).text
    soup = bs4.BeautifulSoup(r, 'html5lib')
    img_page = soup.find('div', {'itemprop' : 'blogPost'}).find_all('img')
    img_src = img_page[0].get('src', '')
    img_gallery = soup.find('a', {'class':'sigProLink fancybox-gallery', 'href':True})
    if img_gallery:
        href = img_gallery.get('href', '')
        if '.jpg' in href:
            img_link = '***GALLERY*** ' + href
            site_img.append(img_link)
            print(img_link)
    else:                               
        try:
            if '.jpg' in img_src:
                    img_link = '**PAGE*** ' + img_src       
                    site_img.append(img_link)
                    print(img_link)
        except IndexError:
                nf = 'No Images Found'
                site_img.append(nf)
                print('nf')

1 个答案:

答案 0 :(得分:1)

尝试将错误处理语句放在一行:

function max_grouped_price( $price_this_get_price_suffix, $instance, $child_prices ) { 

    return wc_price(array_sum($child_prices)); 
} 

add_filter( 'woocommerce_grouped_price_html', 'max_grouped_price', 10, 3 );

根据错误,您将获得一个空的 img_page 数组。