【问题标题】:Beautiful soup doesn't see part of the html file美汤看不到部分html文件
【发布时间】:2022-01-20 16:30:49
【问题描述】:

我无法从 HTML 的输入元素中获取 max_value。

link = 'http://www.bcra.gob.ar//PublicacionesEstadisticas/Principales_variables_datos_i.asp?serie=7935&detalle=Monetary Policy Rate (APR %)'
html_data = requests.get(link).text
soup_direct_link = BeautifulSoup(html_data, 'lxml')
max_start_date = soup_direct_link.find('form', class_ = 'form-inline').input['max']

我尝试了一些可以在 Internet 上找到的其他方法,但没有任何帮助。 此外,还有非常熟悉的页面,如下所示,成功运行:

'http://www.bcra.gob.ar//PublicacionesEstadisticas/Principales_variables_datos_i.asp?serie=246&detalle=BCRA 国际储备(以百万美元为单位 - 临时数字受估值变化影响'

有人知道为什么会这样吗?

【问题讨论】:

  • 你想从页面中提取什么?
  • 选择最小和最大日期范围后弹出的数据。

标签: python beautifulsoup python-requests


【解决方案1】:

如果您检查页面源,您将看到该类不存在。相反,您可以按如下方式使用属性选择器:

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('http://www.bcra.gob.ar//PublicacionesEstadisticas/Principales_variables_datos_i.asp?serie=246&detalle=BCRA', headers = {'User-Agent':'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
print(soup.select_one('[max]')['max'])

【讨论】:

    【解决方案2】:

    您可以尝试使用 Selenium 的 Webdriver 来首先获取“完整”源代码。似乎请求对象没有完全呈现的 html 代码。这可能是由于某些脚本没有使用请求方法“加载”:

    from bs4 import BeautifulSoup
    from selenium import webdriver
    
    link = 'http://www.bcra.gob.ar//PublicacionesEstadisticas/Principales_variables_datos_i.asp?serie=7935&detalle=Monetary Policy Rate (APR %)'
    
    #Download the chrome drivers and provide their location instead of "XYZ"
    driver = webdriver.Chrome(executable_path=r'C:\XYZ\chromedriver.exe')
    driver.get(link)
    
    #Now you can access the code rendered by the Chrome webdriver: 
    soup_direct_link = BeautifulSoup(str(driver.page_source), 'lxml')
    
    max_start_date = soup_direct_link.find('form', class_ = 'form-inline').input["max"]
    print(max_start_date)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 2012-12-19
      • 2016-11-04
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      相关资源
      最近更新 更多