Python 填写并提交html表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12756443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:48:07  来源:igfitidea点击:

Fill and submit html form

pythonpython-2.7

提问by IrfanM

I am trying / wanting to write a Python script (2.7) that goes to a form on a website (with the name "form1") and fills in the first input-field in said form with the word hello, the second input-field with the word Ronald, and the third field with [email protected]

我正在尝试/想要编写一个 Python 脚本(2.7),该脚本转到网站上的表单(名称为"form1"),并用单词填写所述表单中的第一个输入字段,用单词填写hello第二个输入字段Ronald,和第三个字段[email protected]

Can anyone help me code or give me any tips or pointers on how to do this ?

任何人都可以帮我编写代码或给我任何有关如何执行此操作的提示或指示吗?

回答by David Cain

Take a look at Mechanizeand Selenium. Both are excellent pieces of software that would allow you to automate filling and submitting a form, among other browser tasks.

看看机械化。两者都是出色的软件,可让您自动填写和提交表单以及其他浏览器任务。

回答by K Z

Aside from Mechanize and Selenium David has mentioned, it can also be achieved with Requestsand BeautifulSoup.

除了大卫提到的机械化和硒之外,它还可以通过Requests和来实现BeautifulSoup

To be more clear, use Requeststo send request to and retrieve responses from server, and use BeautifulSoupto parse the response html to know what parameters to send to the server.

为了更清楚,用于Requests向服务器发送请求和从服务器检索响应,并用于BeautifulSoup解析响应html以知道要发送给服务器的参数。

Here is an example script I wrote that uses both Requestsand BeautifulSoupto submit username and password to login to wikipedia:

这是我编写的示例脚本,它同时使用RequestsBeautifulSoup提交用户名和密码以登录维基百科:

import requests
from bs4 import BeautifulSoup as bs


def get_login_token(raw_resp):
    soup = bs(raw_resp.text, 'lxml')
    token = [n['value'] for n in soup.find_all('input')
             if n['name'] == 'wpLoginToken']
    return token[0]

payload = {
    'wpName': 'my_username',
    'wpPassword': 'my_password',
    'wpLoginAttempt': 'Log in',
    #'wpLoginToken': '',
    }

with requests.session() as s:
    resp = s.get('http://en.wikipedia.org/w/index.php?title=Special:UserLogin')
    payload['wpLoginToken'] = get_login_token(resp)

    response_post = s.post('http://en.wikipedia.org/w/index.php?title=Special:UserLogin&action=submitlogin&type=login',
                           data=payload)
    response = s.get('http://en.wikipedia.org/wiki/Special:Watchlist')


Update:

更新:

For your specific case, here is the working code:

对于您的具体情况,这是工作代码:

import requests
from bs4 import BeautifulSoup as bs


def get_session_id(raw_resp):
    soup = bs(raw_resp.text, 'lxml')
    token = soup.find_all('input', {'name':'survey_session_id'})[0]['value']
    return token

payload = {
    'f213054909': 'o213118718',  # 21st checkbox
    'f213054910': 'Ronald',  # first input-field
    'f213054911': '[email protected]',
    }

url = r'https://app.e2ma.net/app2/survey/39047/213008231/f2e46b57c8/?v=a'

with requests.session() as s:
    resp = s.get(url)
    payload['survey_session_id'] = get_session_id(resp)
    response_post = s.post(url, data=payload)
    print response_post.text