我必须从ebay.com抓取数据,我的抓取项是:“汽车的标题”,“制造”,“模型”,“变速器”和“价格”,我已经抓取了所有该项目,但在撰写时代码“传输”,向我显示此错误:
Traceback (most recent call last):
File "C:/Users/Oogway/PycharmProjects/web_scraping1/test.py", line 27, in <module>
trans_of_car = title_trans.text
AttributeError: 'NoneType' object has no attribute 'text'
它不会刮擦“传输”项目! * tip:从“ transmission”中删除一些项目,然后显示错误,并在显示错误后将“ transmission”中的两个项目废弃。
代码:
import requests
from bs4 import BeautifulSoup
import re
url = 'https://www.ebay.com/b/Cars-Trucks/6001?_fsrp=0&_sacat=6001&LH_BIN=1&LH_ItemCondition=3000%7C1000%7C2500&rt=nc&_stpos=95125&Model%2520Year=2020%7C2019%7C2018%7C2017%7C2016%7C2015'
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
ebay_cars = soup.find_all('li', class_='s-item')
for car_info in ebay_cars:
title_div = car_info.find('div', class_='s-item__wrapper clearfix')
title_sub_div = title_div.find('div', class_='s-item__info clearfix')
title_p = title_sub_div.find('span', class_='s-item__price')
title_tag = title_sub_div.find('a', class_='s-item__link')
title_maker = title_sub_div.find('span', class_='s-item__dynamic s- item__dynamicAttributes1')
title_model = title_sub_div.find('span', class_='s-item__dynamic s-item__dynamicAttributes2')
title_trans = title_sub_div.find('span', class_='s-item__dynamic s-item__dynamicAttributes3')
name_of_car = title_tag.text
price_of_car = title_p.text
maker_of_car = title_maker.text
model_of_car = title_model.text
trans_of_car = title_trans.text
答案 0 :(得分:0)
这是因为,此元素在网站中不存在此特定条目,这意味着它是None
。只需将语句放在try , except
子句中,这会导致此错误,这将有所帮助。
在这种情况下...
try:
trans_of_car = title_trans.text
except:
trans_of_car = ''