使用Python和BeautifulSoup废弃彭博网站

时间:2019-05-30 18:36:40

标签: python python-3.x python-3.7

我想在[彭博网站] [1]上进行网上剪贴。

我正在尝试在-https://www.freecodecamp.org/news/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe/上运行任务。

我认为只有Python 2推出时才编写任务。我认为该任务不适用于Python 3。

我尝试过

import urllib2
from bs4 import BeautifulSoup 

import urllib.request 
from bs4 import BeautifulSoup

我也尝试过

page = urllib2.urlopen(quote_page) 

page = urllib.request.urlopen(quote_page).read()

这是我的代码

import urllib.request 
from bs4 import BeautifulSoup

# specify the url
quote_page = 'http://www.bloomberg.com/quote/SPX:IND'

# query the website and return the html to the variable ‘page’
page = urllib.request.urlopen(quote_page).read()

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

这些是我收到的错误消息

Traceback (most recent call last):
  File "C:/Users/HP/Desktop/WebScrapingTest.py", line 16, in <module>
    name = name_box.text.strip() # strip() is used to remove starting and trailing
AttributeError: 'str' object has no attribute 'text'

根据FreeCodeCamp,我应该“能够看到它打印出了标准普尔500指数的当前价格。”

更新

为什么我引用汤?

soup = "BeautifulSoup(page, 'html.parser’)"

我取消引号

soup = BeautifulSoup(page, 'html.parser’)

我收到了错误消息

EOL white scanning string literal 

0 个答案:

没有答案