简体   繁体   English

BeautifulSoup AttributeError: 'NoneType' 对象没有属性 'text'

[英]BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text'

I am trying to learn how to scrape websites and therefore not using an API.我正在尝试学习如何抓取网站,因此不使用 API。 I am trying to scrape eBay's websites and my script runs for a few minutes but stops because of an error stating "AttributeError: 'NoneType' object has no attribute 'text'".我正在尝试抓取 eBay 的网站,我的脚本运行了几分钟,但由于出现“AttributeError: 'NoneType' 对象没有属性 'text'”的错误而停止。 I did my due diligence and search on Google/StackOverflow help but was unable to find any.我做了尽职调查并在 Google/StackOverflow 帮助上搜索,但找不到任何帮助。 The error points to the code below but I cannot figure out how to fix it.错误指向下面的代码,但我不知道如何修复它。

total_sold_price = soup.find('span', {'class': 'vi-qtyS-hot-red'}).text

if total_sold_price:
    total_sold_price = soup.find('span', {'class': 'vi-qtyS-hot-red'}).text

else:
    try:
        total_sold_price = soup.find('a', {'class': 'vi-txt-underline'}).text
    except Exception as e:
        print(e)
        total_sold_price = ""

Entire code: https://pastebin.com/xS4bAwZK完整代码: https : //pastebin.com/xS4bAwZK

Thanks in advance, I greatly appreciate it.提前致谢,我非常感谢。

The issue is soup.find('span', {'class': 'vi-qtyS-hot-red'}) returns None, and then you are trying to extract the text .text of None (hence the error message).问题是soup.find('span', {'class': 'vi-qtyS-hot-red'})返回None,然后您尝试提取None 的文本.text (因此出现错误消息)。 There are a few ways you can handle it.有几种方法可以处理它。 All I did was move around some logic:我所做的只是绕过一些逻辑:

Also, you have it store the text total_sold_price = soup.find('span', {'class': 'vi-qtyS-hot-red'}).text , and if it stores it, you have it do the exact something which is redundant.此外,你让它存储文本total_sold_price = soup.find('span', {'class': 'vi-qtyS-hot-red'}).text ,如果它存储它,你让它做一些确切的事情这是多余的。

Lastly, the page is dynamic, so you might want to look into API or other ways to access the data.最后,页面是动态的,因此您可能需要查看 API 或其他访问数据的方式。

But for your code provided, maybe something like this instead:但是对于您提供的代码,可能是这样的:

try:
    total_sold_price = soup.find('span', {'class': 'vi-qtyS-hot-red'}).text
except Exception as e:
    try:
        total_sold_price = soup.find('a', {'class': 'vi-txt-underline'}).text
    except Exception as e:
                print(e)
                total_sold_price = ""

Is it solved?解决了吗? Instead of {'class': 'vi-qtyS-hot-red'} You can write = 'vi-qtyS-hot-red'而不是 {'class': 'vi-qtyS-hot-red'} 你可以写 = 'vi-qtyS-hot-red'

No need write 'class' and {}无需编写 'class' 和 {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BeautifulSoup“AttributeError: 'NoneType' 对象没有属性 'text'” - BeautifulSoup "AttributeError: 'NoneType' object has no attribute 'text'" AttributeError:“NoneType”对象没有属性“文本”-Beautifulsoup - AttributeError: 'NoneType' object has no attribute 'text' - Beautifulsoup AttributeError: 'NoneType' 对象没有属性 'text' BeautifulSoup - AttributeError: 'NoneType' object has no attribute 'text' BeautifulSoup BeautifulSoup: AttributeError: 'NoneType' 对象没有属性 'text' - BeautifulSoup: AttributeError: 'NoneType' object has no attribute 'text' AttributeError: 'NoneType' object 在使用 BeautifulSoup 时没有属性 'text' - AttributeError: 'NoneType' object has no attribute 'text' when using BeautifulSoup AttributeError: 'NoneType' object 没有属性 'text' BeautifulSoup 解析 - AttributeError: 'NoneType' object has no attribute 'text' BeautifulSoup Parsing AttributeError: 'NoneType' object 没有属性 'text' - BeautifulSoup 到 CSV - AttributeError: 'NoneType' object has no attribute 'text' - BeautifulSoup to CSV AttributeError:'NoneType'对象没有属性'text'-Python,BeautifulSoup错误 - AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error AttributeError:'NoneType'对象没有属性'text'beautifulsoup python - AttributeError: 'NoneType' object has no attribute 'text' beautifulsoup python AttributeError:“ NoneType”对象在具有beautifulsoup的Python中没有属性*** - AttributeError: 'NoneType' object has no attribute *** in Python with beautifulsoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM