如何单击表单中的提交按钮? [英] How to click submit button in a form?

查看:107
本文介绍了如何单击表单中的提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过提供开始日期,结束日期并通过POST方法单击获取价格"按钮来删除此网站以获取基金价格历史记录.

I am trying to scrap this website for fund price history, by providing the start date, end date and click the 'Get Prices' button via POST method.

但是,页面requests.post()返回不包含结果,就好像从未按下过获取价格"按钮一样.这是我通过代码添加的URL:

However the page requests.post() return does not contain the results, as if the "Get Price" button was never pressed. This is the URL I put together through the code:

我在Stackoverflow上阅读了其他有关通过POST在Python中提交表单的文章,但我无法使其正常工作.能否请你帮忙?非常感谢!

I read other posts on Stackoverflow about submitting form via POST in Python and I couldn't make it work. Could you please help? Many thanks!

import requests
import datetime

startDate = datetime.datetime(2016,1,1).strftime('%m/%d/%Y')
endDate = datetime.datetime(2016,2,20).strftime('%m/%d/%Y')

serviceurl = 'https://www.nysaves.org/nytpl/fundperform/fundHistory.do?'
payload = {'fundid':1003022, 'startDate':startDate, 'endDate': endDate, 'submit':'Get Prices'}
r = requests.post(serviceurl, params=payload)

#from IPython.core.display import HTML
#HTML(r.content.decode('utf-8'))

推荐答案

  1. 您需要使用数据",而不是参数". "params"用于编码到URL中的GET参数,但是"data"在正文中发送.
  2. 正确的URL是https://www.nysaves.org/nytpl/fundperform/fundHistorySearch.do,而不是https://www.nysaves.org/nytpl/fundperform/fundHistory.do?.
  3. 您不需要submit关键字.但是添加它不会有伤害.
  1. You need to use "data", not "params". "params" is used for GET parameters encoded into the URL, but "data" is sent in the body.
  2. The correct url is https://www.nysaves.org/nytpl/fundperform/fundHistorySearch.do, not https://www.nysaves.org/nytpl/fundperform/fundHistory.do?.
  3. You don't need the submit keyword. But adding it won't hurt.

此代码将起作用:

startDate = datetime.datetime(2016,1,1).strftime('%m/%d/%Y')
endDate = datetime.datetime(2016,2,20).strftime('%m/%d/%Y')

serviceurl = 'https://www.nysaves.org/nytpl/fundperform/fundHistorySearch.do'
payload = {'fundid': 1003022, 'startDate': startDate, 'endDate': endDate}
r = requests.post(serviceurl, data=payload)
print(r.text)

这篇关于如何单击表单中的提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆