用BeautifulSoup刮网站

2024-05-16 06:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着用BeautifulSoup浏览一个网站。更具体地说,我试图从以下标记中获取字符串:

<td class="Fz(s) Fw(500) Ta(end)" data-reactid=".17c0h26fqwq.1.$0.0.0.3.1.$main-0-Quote-Proxy.$main 0-Quote.2.0.0.0.1.0.0:$VALUATION_MEASURES.0.1.0.$MARKET_CAP_INTRADAY.1">4.39B</td>

但是,当我试图查找所有td标签的属性时,beauthulsoup找不到我想要的。代码如下:

^{pr2}$

这是输出:

{'class': ['W(100%)', 'Va(t)', 'Px(0)'], 'data-reactid': '.odbtogw33w.0.0.$uh.2.0.1.0.1.0.0.0'}
{'class': ['Va(t)', 'Tren(os)', 'W(10%)', 'Whs(nw)', 'Px(0)', 'Bdcl(s)'], 'data-reactid': '.odbtogw33w.0.0.$uh.2.0.1.0.1.0.0.1'}

所以,它找不到'class':['Fz(s)''Fw(500)''Ta(end)']

有人知道为什么吗?在

戈兰


Tags: 字符串标记data网站mainclasstdend
2条回答

所以这是我写的附加代码,现在我可以很好地保存动态生成的内容,并使用BeautifulGroup获得我想要的标记:

from contextlib import closing
from selenium.webdriver import Firefox
from selenium.webdriver.support.ui import WebDriverWait

with closing(Firefox()) as browser:
    browser.get('https://finance.yahoo.com/quote/IONS?p=IONS')
    button = browser.find_element_by_link_text('Statistics')
    button.click()
    #WebDriverWait(browser, timeout=10).until(
        #lambda x: x.find_element_by_class_name('Fz(s) Fw(500) Ta(end)'))
    page_source = browser.page_source
print(page_source)

yahoo_finance = BeautifulSoup(page_source, 'html.parser')

@neftes@Padraic Cunningham谢谢你的提示。在

只需使用请求就可以获得数据,内容是从ajax get tohttps://query1.finance.yahoo.com/v10/finance/quoteSummary/IONS生成的:

from pprint import pprint as pp
import requests

params = {"formatted": "true", "lang": "en-US", "region": "US",
          "modules": "defaultKeyStatistics,financialData,calendarEvents", "corsDomain": "finance.yahoo.com"}

url = "http://finance.yahoo.com/quote/IONS/key-statistics?p=IONS"
ajax = "https://query1.finance.yahoo.com/v10/finance/quoteSummary/IONS"

with requests.Session() as s:
    cont = requests.get(url).content
    data = s.get(ajax, params=params).json()

    pp(data[u'quoteSummary']["result"])

这给了你:

^{pr2}$

相关问题 更多 >