使用python提交带有liburl或请求的Web表单

编程入门 行业动态 更新时间:2024-10-28 13:26:37
本文介绍了使用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 = '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 = '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 = '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表单

本文发布于:2023-08-04 22:56:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1300336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   python   liburl   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!