Python BeautifulSoup-处理变量.find().string返回空的情况

Python BeautifulSoup-处理变量.find().string返回空的情况,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我有上面的代码,它解析一些html代码并生成一个管道描述文件。它工作得很好,只是有几个条目的html代码中缺少一个元素(SellerPhone)。并非所有条目都有卖家电话号码 from bs4 import BeautifulSoup import codecs import sys import urllib.request site_response= urllib.request.urlopen("http://site/") html=site_response.read() file

我有上面的代码,它解析一些html代码并生成一个管道描述文件。它工作得很好,只是有几个条目的html代码中缺少一个元素(SellerPhone)。并非所有条目都有卖家电话号码

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)
我在这里失败了。当卖家的电话不见了,线路就断了,我一点也不奇怪。我得到的'AttributeError'非类型对象没有属性字符串。

我可以在没有“.string”的情况下执行“item.find”,并返回完整的html块。但我不知道如何提取这些案例的文本

正确,如果未找到元素,则返回
None

您可以只放置一个
if/else
子句来避免这种情况:

item.find("span", class_="seller-phone").string
或者,如果您喜欢,请使用
try/except
块:

for item in fulllist:
    span = item.find("span", class_="modelYearSort")
    if span:
        item_print = span.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.

希望这有帮助

谢谢,这很有帮助。。。我想如果电话号码不存在,我就不太清楚我想做什么。实际上,我不想跳到下一项,我只想将其视为null,这样我的字符串在该位置就有了| |。然而,我认为我可以利用您在上面提供的内容来实现这一点,因为错误处理部分是我遇到困难的地方。我稍后会尝试一下,也许只需尝试一下
item\u print=item.find('span',class='modelYearSort',text=True)
。。。看看这是否有效-这应该只返回具有非空字符串的节点with@Jon嗯。。“我以为问题是因为BS找不到跨度?”艾基德叹了口气说是的。。。我想我再要一杯咖啡:)我可以看出find的问题,但在列表理解中find_all()更糟糕,因为它崩溃了。我看不出有什么办法可以用一个except子句或者甚至在理解中把它套住。当开发人员忘记标记表中的最后一列时,问题就出现了。查找表(TR)。find(“Th”)。
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)