如何点击表单中的提交按钮?

时间:2016-02-23 05:20:39

标签: python forms python-3.x post python-requests

我试图通过提供开始日期,结束日期并点击“获取价格”按钮通过POST方法废弃此网站的基金价格历史记录。

然而,页面的request.post()返回不包含结果,就好像从未按下“获取价格”按钮一样。这是我通过代码放在一起的URL:

https://www.nysaves.org/nytpl/fundperform/fundHistory.do?submit=Get+Prices&endDate=02%2F20%2F2016&fundid=1003022&startDate=01%2F01%2F2016

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

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 个答案:

答案 0 :(得分:4)

  1. 您需要使用"数据"而不是" params"。 " PARAMS"用于编码到URL中的GET参数,但是"数据"被送到身体里。
  2. 正确的网址为https://www.nysaves.org/nytpl/fundperform/fundHistorySearch.do,而不是https://www.nysaves.org/nytpl/fundperform/fundHistory.do?
  3. 您不需要submit关键字。但添加它不会受到伤害。
  4. 此代码可以使用:

    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)
    
相关问题