使用Python请求选择表单 [英] Using Python Requests to Select Forms

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

问题描述

我想使用一个能够填写表格并处理重定向的python库:

I would like to use a python library capable of filling out forms and handling redirects:

  1. 主页"页面的格式为{'username':'user', 'password':'pass'}
  2. 重定向"页面将我带到新页面
  3. 新"页面上有指向最后一页的链接
  4. 最终"页面的格式为{'Field 1':'Data 1', 'Field 2':'Data 2'}
  1. The "home" page has a form {'username':'user', 'password':'pass'}
  2. The "redirect" page brings me to a new page
  3. The "new" page has a link to the final page
  4. The "final" page has a form {'Field 1':'Data 1', 'Field 2':'Data 2'}

我想进入最终"页面并填写表格.我已经在SO的每篇文章中查找了,请阅读API文档和整个用户指南.

I would like to get to the "final" page and fill out the form. I have already looked through every post in SO for python-requests, read the API doc and the entire user-guide.

我已经能够使用机械化来填写主页"页面表格:

I have been able to use mechanize to fill out "home" page forms:

import mechanize
# Fill out Log In form
br = mechanize.Browser()
br.open('http://www.yourfavoritesite.com')
br.select_form(nr=0)
br['username'] = 'user'
br['password'] = 'pass'
br.submit()

另外-在禁用网页上的重定向后-我已经能够使用机械化来填写新"页面表格:

Additionally -- after disabling the redirect on the webpage -- I have been able to use mechanize to fill out "new" page forms:

# Click link
br.find_link(text='Admin')
req = br.click_link(text='Admin')
br.open(req)

# Fill out Final form
br.select_form(nr=0)
br['Field 1'] = 'Data 1'
br['Field 2'] = 'Data 2'
br.submit()

如果我不禁用重定向并且页面重定向会发生什么,那是我没有进入新"页面,并且当我尝试填写表格时出现以下错误:

What happens if I don't disable the redirect and the page redirects is I don't make it to the "new" page and when I try to fill out the form I get the following error:

File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 524, in select_form
     raise FormNotFoundError("no form matching "+description)
mechanize._mechanize.FormNotFoundError: no form matching nr 0

我听说Python请求非常简单,我想使用此库,前提是我可以按照以下方式做一些事情:

I have heard that Python Requests is very simple and I would like to use this library assuming I could do something along the line of:

import requests
# Fill out Log In form
data = {'username':'admin', 'password':'pass'}
r = requests.get('http://www.yourfavoritesite.com', allow_redirects=True)
r = requests.put(r.url, data=data)

# Follow redirect to "new" page

# Click link
# I haven't heard of this feature in requests

# Fill out Final form
data = {'Field 1':'Data 1', 'Field 2':'Data 2'}
r = requests.put(r.url, data=data)

推荐答案

我知道这很旧,但我相信您从requests寻找的答案(显然)不是get,而是post

I know this is old but I believe that the answer you're looking for from requests is (obviously) not get, but post.

来自: http://docs.python-requests. org/en/latest/user/quickstart/#more-complicated-post-requests

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
{
  ...
  "form": {
    "key2": "value2",
    "key1": "value1"
  },
  ...
}

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

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