python - BeautifulSoup - 处理 variable.find().string 返回空的情况

标签 python python-3.x beautifulsoup

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 代码中缺少其中一个元素(卖家电话)。并非所有条目都有卖家电话号码。

item.find("span", class_="seller-phone").string

我在这里失败了。当卖家电话丢失时,线路会出现故障,我并不感到惊讶。我得到 'AttributeError' NoneType object has not attribute string.

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

最佳答案

你是对的,soup.find如果未找到元素,则返回 None

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

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.

或者,如果您愿意,可以使用 try/except block :

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)

希望这对您有所帮助!

关于python - BeautifulSoup - 处理 variable.find().string 返回空的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442151/

相关文章:

javascript - (Web)套接字连接发送 header 而不是字符串

python - 创建一个以另一个 DataFrame 为条件的 Pandas DataFrame

python - 如何在没有过滤器的情况下使用 tweepy

python - Python 中的合并循环和打印语句

python - 忽略 Popen 的返回值是否安全?

python - psycopg2 - 如何将 NULL 类型更改为字符串 'NA' 而不是 None

python-3.x - py.test 参数化 fixture

python - 如何使用beautifulsoup检查字符串是否存在

python - 查看到到网的抓取表单

python - BeautifulSoup 检索图像 src 属性并进行比较时出现问题