美丽的汤看不到html文件的一部分



我很难从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']

我尝试了一些可以在互联网上找到的其他方法,但没有任何帮助。此外,还有一个非常熟悉的页面,就是下面,工作成功:

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

有人知道为什么会发生这种事吗?

如果检查页面源代码,您会发现该类不存在。相反,您可以使用如下属性选择器:

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'])

您可以尝试使用Selenium中的Web驱动程序来首先获取"满";源代码。requests对象似乎没有完全呈现的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:XYZchromedriver.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)

最新更新