我正在尝试使用 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:
我也试过像这样获取所有 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/