【发布时间】:2016-03-31 07:17:49
【问题描述】:
我正在尝试从 Billboard 前 100 名中获取歌曲的标题。 图片是他们的html脚本。
我写了这段代码:
from bs4 import BeautifulSoup
import urllib.request
url= 'http://www.billboard.com/charts/year-end/2015/hot-100-songs'
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page.read(), "html.parser")
songtitle = soup.find("div", {"class": "row-title"}).h2.contents
print(songtitle)
它检索第一个标题“UPTOWN FUNK!”
当我使用find_all 时,它给了我错误:
line 6, in <module>
songtitle = soup.find_all("div", {"class": "row-title"}).h2.contents
AttributeError: 'ResultSet' object has no attribute 'h2'
为什么它给我一个错误而不是给我所有的标题?完整的 html 脚本可以在这个网站上使用 chrome 中的 Control Shift J 找到:http://www.billboard.com/charts/year-end/2015/hot-100-songs
【问题讨论】:
标签: python html python-3.x beautifulsoup html-parsing