解决 Beautiful Soup 找不到元素时的错误

在使用 Beautiful Soup 解析 HTML 代码并提取数据时,可能会遇到某些元素缺失的情况。这会导致代码在执行find()方法时抛出 AttributeError 异常,因为NoneType 对象没有string属性。

以下代码举例说明了这个问题:

from bs4 import BeautifulSoup
import codecs
import sys

import urllib.request
site_response= urllib.request.urlopen("http://site/")
html=site_response.read()
file = open ("cars.html","wb") #open file in binary mode
file.write(html)
file.close()


soup = BeautifulSoup(open("cars.html"))
output = (soup.prettify('latin'))
#print(output) #prints whole file for testing

file_output = open ("cars_out.txt","wb")
file_output.write(output)
file_output.close()

fulllist=soup.find_all("div", class_="row vehicle")
#print(fulllist) #prints each row vehicle class for debug

for item in fulllist:
    item_print=item.find("span", class_="modelYearSort").string
    item_print=item_print + "|" + item.find("span", class_="mmtSort").string
    seller_phone=item.find("span", class_="seller-phone")
    print(seller_phone)
    # item_print=item_print + "|" + item.find("span", class_="seller-phone").string
    item_print=item_print + "|" + item.find("span", class_="priceSort").string
    item_print=item_print + "|" + item.find("span", class_="milesSort").string
    print(item_print)

这段代码用于解析一个包含汽车信息的 HTML 文件,并提取每个汽车的型号、名称、卖家电话、价格和里程信息。但是,如果 HTML 文件中存在缺少卖方电话号码的汽车信息,则会出现AttributeError异常。

2、解决方案

为了解决这个问题,可以使用以下两种方法:

方法一:使用 if/else 语句

for item in fulllist:
    if item.find("span", class_="seller-phone"):
        item_print=item.find("span", class_="modelYearSort").string
        item_print=item_print + "|" + item.find("span", class_="mmtSort").string
        seller_phone=item.find("span", class_="seller-phone")
        print(seller_phone)
        # item_print=item_print + "|" + item.find("span", class_="seller-phone").string
        item_print=item_print + "|" + item.find("span", class_="priceSort").string
        item_print=item_print + "|" + item.find("span", class_="milesSort").string
        print(item_print)
    else:
        continue #It's empty, go on to the next loop.

这种方法使用if/else语句来判断seller_phone元素是否存在。如果存在,则提取卖方电话号码并将其添加到item_print字符串中。如果不存在,则跳过当前循环,继续处理下一个汽车信息。

方法二:使用 try/except 语句

for item in fulllist:
    try:
        item_print=item.find("span", class_="modelYearSort").string
    except AttributeError:
        continue #skip to the next loop.
    else:
        item_print=item_print + "|" + item.find("span", class_="mmtSort").string
        seller_phone=item.find("span", class_="seller-phone")
        print(seller_phone)
        # item_print=item_print + "|" + item.find("span", class_="seller-phone").string
        item_print=item_print + "|" + item.find("span", class_="priceSort").string
        item_print=item_print + "|" + item.find("span", class_="milesSort").string
        print(item_print)

这种方法使用try/except语句来捕获AttributeError异常。如果发生异常,则说明当前汽车信息没有卖方电话号码,因此跳过当前循环,继续处理下一个汽车信息。如果未发生异常,则说明当前汽车信息存在卖方电话号码,因此将其提取并添加到item_print字符串中。

希望本文对您有所帮助!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值