AttributeError:抓取 Ebay 产品标题时,“NoneType”对象没有属性“text”

问题描述 投票:0回答:1

按照本教程使用 Python 创建 Ebay 价格跟踪器,在尝试从 Ebay 的搜索结果页面获取产品标题时,我遇到了 AttributeError: 'NoneType' 对象没有属性 'text'。

这个班级是正确的,正如您在这里看到的:

'title': item.find('h3', {'class': 's-item__title s-item__title--has-tags'}).text,
知道为什么我会收到此错误以及如何绕过它吗?

完整代码如下:

import requests from bs4 import BeautifulSoup import pandas as pd searchterm = 'screen' def get_data(searchterm): url = f'https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2380057.m570.l1313&_nkw={searchterm}&_sacat=0&LH_PrefLoc=1&LH_Auction=1&rt=nc&LH_Sold=1&LH_Complete=1' r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') return soup def parse(soup): productslist = [] results = soup.find_all('div',{'class': 's-item__info clearfix'}) for item in results: product = { 'title': item.find('h3', {'class': 's-item__title s-item__title--has-tags'}).text, 'soldprice': float(item.find('span', {'class': 's-item__price'}).text.replace('$','').replace(',','').strip()), 'solddate': item.find('span', {'class': 's-item__title--tagblock__COMPLETED'}).find('span',{'class': 'POSITIVE'}.text), 'bids': item.find('span', {'class': 's-item__bids'}).text, 'link': item.find('a', {'class': 's-item__link'})['href'], } productslist.append(product) return productslist def output (productslist, searchterm): productsdf = pd.DataFrame(productslist) productsdf.to_csv(searchterm + 'ebaytrackeroutput.csv', index=False) print('Saved to CSV') return soup = get_data(searchterm) productslist = parse(soup) output(productslist, searchterm)
谢谢您的帮助!

python screen-scraping
1个回答
1
投票
有些商品没有标题或售价。他们不会返回任何东西。你会得到这个错误。所以你需要跳过它们。

另一件事是

item.find('span', {'class': 's-item__title--tagblock__COMPLETED'})

这条线总是不返回任何内容。所以你需要检查一下原因。

学习:

如何调试小程序

import requests from bs4 import BeautifulSoup import pandas as pd searchterm = 'screen' def get_data(searchterm): url = f'https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2380057.m570.l1313&_nkw={searchterm}&_sacat=0&LH_PrefLoc=1&LH_Auction=1&rt=nc&LH_Sold=1&LH_Complete=1' r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') return soup def parse(soup): productslist = [] results = soup.find_all('div',{'class': 's-item__info clearfix'}) for item in results: title = item.find('h3', {'class': 's-item__title s-item__title--has-tags'}) soldprice = item.find('span', {'class': 's-item__price'}) if title == None or soldprice == None: # if these are none just skip them. continue # solddate is always returning none you need to check why product = { 'title': title.text, 'soldprice': float(soldprice.text.replace('$','').replace(',','').strip()), # 'solddate': item.find('span', {'class': 's-item__title--tagblock__COMPLETED'}).find('span',{'class': 'POSITIVE'}.text), 'bids': item.find('span', {'class': 's-item__bids'}).text, 'link': item.find('a', {'class': 's-item__link'})['href'], } productslist.append(product) return productslist def output (productslist, searchterm): productsdf = pd.DataFrame(productslist) productsdf.to_csv(searchterm + 'ebaytrackeroutput.csv', index=False) print('Saved to CSV') return soup = get_data(searchterm) productslist = parse(soup) output(productslist, searchterm)
    
© www.soinside.com 2019 - 2024. All rights reserved.