简体   繁体   中英

How to use Mechanize to fill HTML forms in Python

I'm new to mechanize, and i don't quite understand how does it work, I tried a lot of tutorials, but most of them were outdated and didn't work.

First question is, What effect does Mechanize make? does it fill forms in specific browser so end-users can see it, Or does it make everything in Mechanize browser that cannot be seen by end-user?

I'm trying to make Mechanize fill out the form, Form changes input name after reloading page, How can i change its value by number?

import mechanize 

br = mechanize.Browser() 

br.set_handle_robots(False) 

br.addheaders = [("User-agent","Mozilla/5.0")] 

gitbot = br.open("https://arkhamnetwork.org/community/register") 

br.select_form(nr=0)

br["user[username]"] = "username" 

br["user[email]"] = "email"     

br["user[password]"] = "password"

sign_up = br.submit()

Error i am getting after executing code: NameError: name 'username' is not defined

I want to fill out all the forms on the page, without using input name, How can i do it?

I have found solution:

Forms are actually containing controls, Thats why i needed to select form.

Code that fills out forms on this specific website:

import mechanize 

br = mechanize.Browser() 

response = br.open("https://arkhamnetwork.org/community/register")

br.addheaders = [("User-agent","Mozilla/5.0")] 

gitbot = br.open("https://arkhamnetwork.org/community/register") 

br.select_form(nr=1)

br.set_all_readonly(False)

br.form.set_value("test", nr=0)

br.method = "POST"

response = br.submit()

print response.geturl('http://arkhamnetwork.org/community/register/register')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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