使用python提交带有liburl或请求的Web表单 [英] Using python to submit a web form with liburl or requests

查看:65
本文介绍了使用python提交带有liburl或请求的Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网页上提交表单,并转到下一页的html.提交表单后的页面.我发现有两种使用请求或liburl进行此操作的方法.

I am trying to submit a form on the webpage and get to the html of the next page after the form is submitted. I found two ways of doing this using either requests or liburl.

import urllib
import urllib2
import webbrowser

url = 'https://fcq.colorado.edu/UCBdata.htm'

def main():
    data = urllib.urlencode({'subj': 'CSCI', 'crse': '1300'})
    results = urllib2.urlopen(url, data)
    print results.read()
    with open("results.html", "w") as f:
        f.write(results.read())

    webbrowser.open("results.html")
    return 0

 if __name__ == '__main__':
    main()

或:

import requests

url = 'https://fcq.colorado.edu/UCBdata.htm'

def main():     
    payload = {'subj': 'CSCI', 'crse': '1300'}
    r = requests.post(url, payload)
    with open("requests_results.html", "w") as f:
        f.write(r.content)  
    return 0

if __name__ == '__main__':
    main()

当我在请求后得到页面时,这是在两种方法上都具有表单的页面.我想知道是否可能需要使用提交"按钮做些什么?我是Web和python的新手,所以任何提示或想法都将不胜感激.谢谢!

When I get the page after the request it is just the same page that has the form on it for both methods. I was wondering if I maybe had to do something with the submit button? I am new to web stuff and python so any tips or ideas would be greatly appreciated. Thanks!

这是提交"按钮的html:

Here is the html of the submit button:

<input type="submit" name="sub" value="Submit Request" onclick="this.disabled=true,this.form.submit();">

推荐答案

该页面上的表单实际上是由JavaScript提交的,因此仅查看<form />元素是不够的.您可以使用例如提交表单后, Firebug 的网络标签或Chrome开发人员工具可检查POST请求,以查看实际内容提交.

The form on that page is will actually be submitted by JavaScript, so just looking at the <form /> element is not (necessarily) enough. You can use e.g. Firebug's network tab or the Chrome developer tools to inspect the POST request after you submit the form in order to see what's actually submitted.

这似乎可行:

import requests

url = 'https://fcq.colorado.edu/scripts/broker.exe'

payload = {
    "_PROGRAM": "fcqlib.fcqdata.sas",
    "_SERVICE": "fcq",
    "camp": "BD",
    "fileFrmt": "HTM",
    "ftrm": "1",
    "fyr": "2007",
    "grp1": "ALL",
    "jjj": "mytst",
    "ltrm": "7",
    "lyr": "2013",
    "sort": "descending YEARTERM SUBJECT COURSE SECTION",
}

payload.update({
    'subj': 'CSCI',
    'crse': '1300',
})


def main():
    r = requests.post(url, payload)
    with open("requests_results.html", "w") as f:
        f.write(r.content)
    return 0

if __name__ == '__main__':
    main()

这篇关于使用python提交带有liburl或请求的Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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