javascript - 使用 BeautifulSoup 在 div 下的 li 中找不到文本

标签 javascript python web-scraping beautifulsoup

我正在尝试使用 BeautifulSoup 获取本网站 div 下 ul 中的文本:https://www.nccn.org/professionals/physician_gls/recently_updated.aspx

但我只得到一个空的 div。我的代码是:

page = requests.get("https://www.nccn.org/professionals/physician_gls/recently_updated.aspx")

soup=BeautifulSoup(page.content,"html.parser")

_div=soup.find("div",{"id":"divRecentlyUpdatedList"})

element = [i.text for i in b.find("a") for b in _div.find("ul")]

结果是:

HTML文件截图如下:div and ul

此外,在我试图从中获取内容的 div 之后有 javascript:

div and javascript

我也试过像这样获取所有 li:

l = []
for tag in soup.ul.find_all("a", recursive=True): 
    l.append(tag.text)

但是我得到的文字不是我想要的。该 div 下的文本是否被 javascript 隐藏?

欢迎任何帮助。非常感谢您。

最佳答案

内容从端点 https://www.nccn.org/professionals/physician_gls/GetRecentlyUpdated.ashx 异步填充到 HTML 中,返回 JSON。由于它是通过 JS 异步填充的,requests 看不到它的结果。

您可以直接请求该端点并改为解析 JSON,例如:

page = requests.get("https://www.nccn.org/professionals/physician_gls/GetRecentlyUpdated.ashx")
list = json.loads(page.content)
for item in list['recent_guidelines']:
    print(item['Name'], item['VersionNumber'], item['PublishedDate'])

关于javascript - 使用 BeautifulSoup 在 div 下的 li 中找不到文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61973878/

相关文章:

python-3.x - 使用带有 Python3.x 和 Selenium 的 Selenium 选择复选框

javascript - 将 TypeScript 与外部 JS 文件捆绑在一起(例如 node_modules)

python - 分析使用多处理的 Python 代码?

python - matplotlib 中的子图创建循环

Python:网页抓取标签导航 wiki 表

python - 使用 scrapy 和 Xpath 的空列表

javascript - 如果从上下文菜单调用,Chrome getSelection 不工作

javascript - 防止 react-redux 在状态改变时重新渲染整个页面

javascript - 动画 Google 折线图中的新数据

python - 动态缩进和突出显示 tkinter 文本框中的单词