简体   繁体   English

如何使用python填写和提交表单

[英]How to fill and submit a form using python

I am filling a form of a web page with the help of mechanize module but I am getting error when I run my code.我在机械化模块的帮助下填写网页表单,但在运行代码时出现错误。 I just want to fill the form and submit it successfully.我只想填写表格并成功提交。

My attempt :我的尝试:

code snippet from this stack answer堆栈答案中的代码片段

import re
from mechanize import Browser

username="Bob"
password="admin"
br = Browser()

# Ignore robots.txt
br.set_handle_robots( False )
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Firefox')]

br.open("https://fb.vivoliker.com/app/fb/token")
br.select_form(name="order")
br["u"] = [username]  
br["p"]=[password]

response = br.submit()  

Output : Error (FormNotFoundError)输出:错误(FormNotFoundError)

but what should I enter the name in br.select_form() because when I see source code of web page their is no name attribute set to that form.但是我应该在br.select_form()输入什么名称,因为当我看到网页的源代码时,它们没有为该表单设置名称属性。

Html source code of form from web page来自网页的表单的 Html 源代码

<div class="container">
<form ls-form="fb-init">
<input type="hidden" name="machine_id">
<div class="form-group row">
<input id="u" type="text" class="form-control" placeholder="Facebook Username / Id / Email / Mobile Number" required="required">
</div>
<div class="form-group row">
<input id="p" type="password" class="form-control" placeholder="Facebook Password" required="required">
</div>
<div class="form-group row mt-3">
<button type="button" id='generating' class="btn btn-primary btn-block" onclick="if (!window.__cfRLUnblockHandlers) return false; get()" data-cf-modified-4e9e40fa9e78b45594c87eaa-="">Get Access Token</button>
</div>
<div ls-form="event"></div>
</form>

Expected output : My form should be submit with the values that I given.预期输出:我的表单应该使用我给出的值提交。 see javascript of this webpage given below .请参阅下面给出的此网页的 javascript。 I want to fill and submit form of this web page : Web page source我要填写并提交此网页的表格:网页来源

I believe the form you want to select is ls-form=fb-init我相信你要选择ls-form=fb-initls-form=fb-init

However, since mechanize module requires replacing hyphens with underscores to convert HTML attrs to keyword arguments, you would want to write it like this:但是,由于 mechanize 模块需要用下划线替换连字符以将 HTML 属性转换为关键字参数,因此您可能希望这样编写:

br.select_form(ls_form='fb-init')

To clarify, the correct form to select is not named 'order', the form is named 'fb-init' and it is a ls-form (written as 'ls_form' with underscore).澄清一下,要选择的正确表单不是“order”,而是名为“fb-init”的表单,它是一个 ls 表单(写成带有下划线的“ls_form”)。 So with the change, it should be like this:所以随着变化,它应该是这样的:

import re
from mechanize import Browser

username="Bob"
password="admin"
br = Browser()

# Ignore robots.txt
br.set_handle_robots( False )
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Firefox')]

br.open("https://fb.vivoliker.com/app/fb/token")
br.select_form(ls_form='fb-init')

And then continue from there.然后从那里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM