【问题标题】:beautiful soup- Scraping a site with hidden tag美丽的汤 - 用隐藏标签抓取网站
【发布时间】: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


【解决方案1】:

使用 Selenium,因为它使用 Javascript 并将其传递给 Beautifulsoup。也可以pip install selenium 获取 chromedriver.exe

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.nba.com/game/bkn-vs-cha-0022000032/play-by-play")
soup = BeautifulSoup(driver.page_source, "html.parser")

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多