find返回None时的BeautifulSoup错误处理 [英] BeautifulSoup error handling when find returns NoneType
本文介绍了find返回None时的BeautifulSoup错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从一个网站上抓取搜索结果,其中每个结果都包含在中,并具有与之相关的一系列数据.但是,其中的某些数据值丢失了,当它们丢失时,将返回错误"NoneType"对象没有属性"text".
I am scraping search results from a website where each result is contained in a and has a range of data associated with it. However some of these data values are missing and when they are, the error ''NoneType' object has no attribute 'text'' is returned.
我放入了try/except块.当前,当缺少其中一个值时,将跳过整个搜索结果.我该怎么做才能将丢失的值替换为要保存到的xls文件中的"或空白?
I have put in a try/except block. Currently the entire search result is skipped when one of the values is missing. What can I do to allow the missing values to be replaced with a "", or blank in the xls file I am saving to?
我的代码如下:
divs = soup.find_all("div", class_="result-item standard") + soup.find_all("div", class_="result-item standard basic-ad")
for div in divs:
try:
#item_title = " ".join(div.h2.a.text.split())
item = div.h2.a.text.split()
item_year = item[0]
item_make = item[1]
item_model = ""
for i in range (2,len(item)):
item_model = item_model + item[i] + " "
item_eng = div.find("li", "item-engine").text
item_trans = div.find("li", "item-transmission").text
item_body = div.find("li", "item-body").text
item_odostr = div.find("li", "item-odometer").text
item_odo = ''.join(c for c in item_odostr if c.isdigit())
item_pricestr = " ".join(div.find("div", "primary-price").text.split())
item_price = ''.join(c for c in item_pricestr if c.isdigit())
item_adtype = div.find("div", "ad-type").span.text
#item_distance = div.find("a", "distance-from-me-link").text
item_loc = div.find("div", "call-to-action").p.text
item_row = (str(x),item_year,item_make,item_model,item_eng,item_trans,item_body,item_odo,item_price,item_adtype,item_loc)
print ",".join(item_row)
print(" ")
for i in range(len(item_row)):
ws.write(x,i,item_row[i])
if x % 500 == 0 :
wb.save("data.xls")
except AttributeError as e:
with open("error"+str(x)+".txt", "w+") as error_file:
error_file.write(div.text.encode("utf-8"))
推荐答案
例如:
item_eng = div.find("li", "item-engine").text if div.find("li", "item-engine") else ''
或:
item_eng = div.find("li", "item-engine").text if len(div.find_all("li", "item-engine"))!=0 else ''
这篇关于find返回None时的BeautifulSoup错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文