【问题标题】:BeautifulSoup Scraping - 'find' method does not return any children in 'div' tagBeautifulSoup Scraping - 'find' 方法不会在 'div' 标记中返回任何子级
【发布时间】:2021-08-19 09:31:25
【问题描述】:
我正在使用 BeautifulSoup 从 [http://financials.morningstar.com/company-profile/c.action?t=AAPL][1] 中提取信息
特别是“操作详细信息”部分中的“CIK”字段,如 [image][1] 所示
这是我使用的代码:
```page = requests.get('http://financials.morningstar.com/company-profile/c.action?t=AAPL')```
```soup = BeautifulSoup(page.content, 'html5lib')```
```div = soup.find(name='div',attrs={'id':'OperationDetails'} ) ```
在 ```print(div)``` 我得到一个空的标签输出。
但是,在检查页面时,带有 **id='OperationDetails'** 的 'div' 标签下确实有子标签。我在这里遗漏了什么吗?
我是使用 BeautifulSoup 的初学者,我正在这个网站上练习。出了什么问题,我现在如何获得包含我正在寻找的信息 (CIK) 的“表格”元素?
衷心感谢。
编辑:真的很抱歉,我不知道为什么 Stackoverflow 会在问题发布后删除图片和网站链接。如果您需要任何其他详细信息,请告诉我,我会尽快回复。再次感谢。
【问题讨论】:
标签:
python
html
beautifulsoup
【解决方案1】:
“操作详情”面板是从外部 URL 加载的。您可以使用此示例如何加载它:
import requests
from bs4 import BeautifulSoup
url = (
"http://financials.morningstar.com/cmpind/company-profile/component.action"
)
query = {
"component": "OperationDetails",
"t": "XNAS:AAPL", # <--- change to your ID
"region": "usa",
"culture": "en-US",
"cur": "",
}
soup = BeautifulSoup(requests.get(url, params=query).content, "html.parser")
cik = (
soup.find(lambda tag: tag.name == "th" and "CIK" in tag.text)
.find_next("td")
.text
)
print(cik)
打印:
320193
【讨论】:
-
谢谢安德烈。我如何知道这样的页面是否具有来自外部 URL 的依赖项?
-
@TheEqualizer 检查源代码,例如print(soup) - 如果您没有看到那里的值,很可能该值是从外部源加载的(您可以在开发者工具中看到所有外部源-> 网络标签)
-
非常感谢安德烈。我真诚地感谢您的快速回复。这很有帮助。