Rat*_*nde 5 python mechanize mechanize-python
我在机械化模块的帮助下填写网页表单,但在运行代码时出现错误。我只想填写表格并成功提交。
我的尝试:
此堆栈答案中的代码片段
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()
Run Code Online (Sandbox Code Playgroud)
输出: 错误(FormNotFoundError)
但是我应该输入什么名称,br.select_form()因为当我看到网页的源代码时,它们没有设置为该表单的名称属性。
来自网页的表单的 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>
Run Code Online (Sandbox Code Playgroud)
预期输出: 我的表单应该使用我给出的值提交。请参阅下面给出的此网页的 javascript。我要填写并提交此网页的表格: 网页来源
I believe the form you want to select is ls-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:
br.select_form(ls_form='fb-init')
Run Code Online (Sandbox Code Playgroud)
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). 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')
Run Code Online (Sandbox Code Playgroud)
And then continue from there.