Python Web爬网:与日历交互

时间:2019-02-06 18:16:11

标签: javascript python html web-scraping

使用以下网址,我已经通过访问iframe对象成功解析了所有表内容。

http://www.b3.com.br/en_us/market-data-and-indices/data-services/market-data/historical-data/derivatives/trading-session-settlements/

但是,我现在试图与页面顶部的日历进行交互,但是没有成功。这样做的目的是能够通过单个python脚本解析不同的数据日期。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

可以通过执行request.post()并使用所需日期设置dData1参数来获得所需表。例如,以下显示了如何显示两个可能的日期。它打印每个日期的前几行数据:

from bs4 import BeautifulSoup
import requests

url = 'http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-ajustes-do-pregao-enUS.asp'
dates = ['02/01/2019', '01/03/2019']

for date in dates:
    req = requests.post(url, {'dData1' : date})
    soup = BeautifulSoup(req.content, "html.parser")

    print(f'\n{date}\n')

    for tr in soup.table.find_all('tr')[1:10]:
        row = [td.get_text(strip=True) for td in tr.find_all('td')]
        print(row)

这将显示以下内容,您可以使用它们进行处理:

02/01/2019

['AFS   - Futures', 'G19', '13,270.000', '13,270.000', '0.000', '0.00']
['', 'H19', '13,356.000', '13,353.100', '-2.900', '29.00']
['', 'J19', '13,407.300', '13,402.000', '-5.300', '53.00']
['', 'K19', '13,459.500', '13,455.200', '-4.300', '43.00']
['ARB   - Argentinian peso', 'G19', '98.4420', '98.4420', '0.0000', '0.00']
['', 'H19', '94.9590', '95.5790', '0.6200', '93.00']
['', 'J19', '92.1780', '92.5210', '0.3430', '51.45']
['', 'K19', '89.3970', '89.8380', '0.4410', '66.15']
['AUD   - Australian dollar', 'G19', '2,662.7830', '2,662.7830', '0.0000', '0.00']

01/03/2019

['AFS   - Futures', 'G19', '14,452.900', '14,332.500', '-120.400', '1,204.00']
['', 'H19', '14,499.900', '14,381.300', '-118.600', '1,186.00']
['', 'J19', '14,555.900', '14,435.000', '-120.900', '1,209.00']
['ARB   - Argentinian peso', 'G19', '97.5170', '96.1230', '-1.3940', '209.10']
['', 'H19', '92.8950', '91.4710', '-1.4240', '213.60']
['', 'J19', '89.9980', '88.7480', '-1.2500', '187.50']
['AUD   - Australian dollar', 'G19', '2,674.0940', '2,638.5800', '-35.5140', '2,130.84']
['', 'H19', '2,681.9240', '2,645.7490', '-36.1750', '2,170.50']
['', 'J19', '2,689.0940', '2,652.6630', '-36.4310', '2,185.86']

正在使用的URL是通过在输入日期时观看页面所请求的URL来找到的。