How to understand this raw HTML of Yahoo! Finance when retrieving data using Python?

后端 未结 2 1206
渐次进展
渐次进展 2021-01-01 02:26

I\'ve been trying to retrieve stock price from Yahoo! Finance, like for Apple Inc.. My code is like this:(using Python 2)

import requests
from bs4 import Bea         


        
2条回答
  •  难免孤独
    2021-01-01 03:13

    Not sure what you mean by 'dynamic' in this case, but have you considered using CSS selectors?

    With Beautifulsoup you could get it e.g like this:

    soup.select('div#quote-header-info section span')[0]  
    

    And there are some variations you could use on the pattern, such as using the '>' filter.

    You could get the same with just lxml, no need for BeautifulSoup:

    import lxml.html as html
    page = html.parse(url).getroot()
    content = page.cssselect('div#quote-header-info section > span:first-child')[0].text
    

    Which immediately illustrates a more specific selector.

    If you're interested in more efficient DOM-traversal, research xpaths.

提交回复
热议问题