Answer a question

I am using BeautifulSoup to extract information from [http://financials.morningstar.com/company-profile/c.action?t=AAPL][1]
Especially, the 'CIK' field from the 'Operation Details' section as shown in the [image][1]

This is the code I have used:

```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'}) ```

Upon ```print(div)``` I get an empty tag output.

However, upon inspecting the page the 'div' tag with **id='OperationDetails'** does have child tags under it. Am I missing something here?

I am a beginner in using BeautifulSoup and I was practicing on this website. What is wrong and how do I now get the 'table' element that has the information (CIK) I am looking for?

Sincere thanks.
edit: I am really sorry, I dont know why Stackoverflow is removing image and website links after the question is posted. Please let me know if you need any additional details, I will be prompt in responding as quickly as I can. Thanks again.

Answers

The "Operation Details" panel is loaded from external URL. You can use this example how to load it:

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)

Prints:

320193
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐