从网站上抓取文字值,但出现AttributeError

时间:2018-09-29 18:29:34

标签: python web-scraping

我正在尝试从Bloomberg网站上抓取文本值“ S&P 500 Index”,但收到错误:

  

回溯(最近通话最近):     在第17行的文件“ tester.py”中       名称= name_box.text.strip()   AttributeError:'NoneType'对象没有属性'text'

enter image description here

我的Python代码:

# -*- coding: utf-8 -*-
#import libraries
import requests
from bs4 import BeautifulSoup
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

#query the website and return the html to the variable ‘page’
page = requests.get('https://www.bloomberg.com/quote/SPX:IND', verify=False)

#parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page.content,'html.parser')

#get the companyName
name_box = soup.find('h1', attrs={'class': 'companyName__99a4824b'})
name = name_box.text.strip()
print(name_box)

#get the index price
price_box = soup.find('div', attrs={'class':'price__c3a38e1d'})
price = price_box.text
print(price)

0 个答案:

没有答案