source_code = requests.get('http://en.wikipedia.org/wiki/Taylor_Swift_discography')
soup = BeautifulSoup(source_code.text)
tables = soup.find_all("table")
我试图从Taylor Swift's discography的“单曲列表”表中获取歌曲名称列表
这个表没有唯一的类或id。我能想到的唯一唯一的东西是“单曲列表…”周围的标题标签
List of singles as main artist, with selected chart positions, sales figures and certifications
我试过了:
^{pr2}$但它什么也不返回,我假设caption不是bs4中可识别的标记?在
这是一个完整的例子,解决了“泰勒-斯威夫特问题”。首先查找包含文本“List of singles”的标题并移动到父对象。下一步迭代包含要查找的文本的项:
这样可以得到:
^{pr2}$它实际上与}中是出于兼容性的考虑,引用
findAll()
和find_all()
无关。findAll()
在BeautifulSoup3
中使用,而留在{bs4
的源代码:而且,有一种更好的方法来获得单曲列表,它依赖于带有} 获取
^{pr2}$id="Singles"
的span
元素,它表示Singles
段落的开始。然后,使用^{span
标记父标记后面的第一个表。然后,使用scope="row"
获取所有th
元素:印刷品:
相关问题 更多 >
编程相关推荐