【发布时间】:2021-06-18 17:06:25
【问题描述】:
我正在尝试逐个游戏桌来抓取 NBA.com 游戏,因此我想获取示例图片中每个框的文本。 例如(https://www.nba.com/game/bkn-vs-cha-0022000032/play-by-play)。
检查 html 代码我发现每一行都在一个包含 div 标签的文章标签中,该标签包含两个 p 标签和我想要的信息,但是我写了以下代码,我发现有 0 篇文章,只有 9 篇P 标签(应该更多)但即使是我得到的标签,它们的文本也不是盒子而是别的东西。我得到了 9 个标签,所以我做错了什么,我不确定它是什么。
这是获取标签的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
def contains_word(t):
return t and 'keyword' in t
url = "https://www.nba.com/game/bkn-vs-cha-0022000032/play-by-play"
page = urlopen(url)
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
div_tags = soup.find_all('div', text=contains_word("playByPlayContainer"))
articles=soup.find_all('article')
p_tag = soup.find_all('p', text=contains_word("md:bg"))
谢谢!
【问题讨论】:
-
似乎这些字段是在使用
js的页面之后加载的。您需要使用selenium之类的东西来提取此信息。
标签: python html web-scraping beautifulsoup