AttributeError:"NoneType"对象在网络抓取中没有属性"text" [英] AttributeError: 'NoneType' object has no attribute 'text' in web-scraping

查看:313
本文介绍了AttributeError:"NoneType"对象在网络抓取中没有属性"text"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此网站上重新创建网络抓取

I'm trying to recreate the web scraping on this website

https://medium.freecodecamp .org/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe

我在jupyter中工作是第一个项目,但我想出了这个错误

I'm working in jupyter as a first project and I've come up with this error

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

AttributeError: 'NoneType' object has no attribute 'text'

我尝试过更改链接,但这没什么区别.我真的不了解该问题的任何处理方法.这是到目前为止的所有代码...

I've tried changing the link but it makes no difference. I don't really know enough to do anything about the problem. Here is all the code so far...

#import the libraries
import urllib.request
from bs4 import BeautifulSoup
# specify the url
quote_page = "https://www.bloomberg.com/quote/SP1:IND"
page = urllib.request.urlopen(quote_page)
# parse the html using BeautifulSoup and store in variable `soup`
soup = BeautifulSoup(page, "html.parser")
# Take out the <div> of name and get its value
name_box = soup.find("h1", attrs={"class": "name"})
name = name_box.text.strip() 
# strip() is used to remove starting and trailing
print (name)
# get the index price
price_box = soup.find("div", attrs={"class":"price"})
price = price_box.text.strip()
print (price)

任何帮助将不胜感激

推荐答案

我使用selenium进行网络抓取,但是我敢肯定(也许)可以为您提供帮助.
本节是您的代码给您带来的错误,我假设:

I use selenium to webscrape, but I am sure I can help you out (maybe).
This section is were you code gives you the error I assume:

price_box = soup.find("div", attrs={"class":"price"})
price = price_box.text.strip()
print (price)

我要做的是:

price_box = soup.find("div", attrs={"class":"price"})
price = price_box().text
print (price.text)

这篇关于AttributeError:"NoneType"对象在网络抓取中没有属性"text"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆