Python:使用请求POST来获取网站数据

时间:2018-11-26 16:30:05

标签: python html forms python-requests

我已经花了很多时间试图通过下面的python数据框从以下网站获取追溯价格:

http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim2/Ajustes2.asp

我能够使用以下方法成功获取当前数据:

url = 'http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim2/Ajustes2.asp'
df = pd.read_html(url, header = 0)

现在我正在为追溯价格而苦苦挣扎:

import pandas as pd
import requests

#BMF webpage
url = 'http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim2/Ajustes2.asp'

#requests to manipulate BMF Form
r = requests.post(url, data = {'txtData':'11/21/2018'})
df_1 = pd.read_html(r.url, header = 0)

我相信我已经在提到的网站上标识了正确的ID:

.
.
.
<h1>Settlement Prices</h1>
<table border="0" cellpadding="0" cellspacing="0">
<form name="frmBD" onSubmit="return retroativo_Validator(this)"action="Ajustes2.asp" method="POST">
<tr>
<td class="TXT_Azul">RETROACTIVE:&nbsp;</td>
<td>
<input name="txtData" MAXLENGTH="10" onKeyPress="javascript:mask_data_home('2','0',true,frmBD);" type="text" class="combo" id="txtData">
<input name="Consultar" type="image" src="http://www.bmf.com.br/bmfbovespa/images/comum/btoOk.gif" border="0" align="absmiddle">
<img src="http://www.bmf.com.br/bmfbovespa/images/comum2/btoLegenda.gif" align="absmiddle" onMouseOver="javascript:legenda.style.display='block';" onMouseOut="javascript:legenda.style.display='none';">
<div id="legenda" style="position:absolute; width:200px; height:115px; z-index:10; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px solid #C9C9C9; overflow: visible; display: none;"> 
.
.
.

我非常感谢您的帮助或见识

谢谢!

2 个答案:

答案 0 :(得分:0)

在您的请求中添加以下标头:

Content-Type: application/x-www-form-urlencoded

答案 1 :(得分:0)

df_1 = pd.read_html(r.url,header = 0)更改为df_1 = pd.read_html(r.text,header = 0),因为您希望在发布请求后获得数据。如果您访问r.url,则会得到最新数据,因为txtData为None。