Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

python - mechanize select form using id

I am working on mechanize with python.

<form action="/monthly-reports"  accept-charset="UTF-8" method="post" id="sblock">

The form here does not have a name. How can I parse the form using it's id?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found this as a solution for the same problem. br is the mechanize object:

formcount=0
for frm in br.forms():  
  if str(frm.attrs["id"])=="sblock":
    break
  formcount=formcount+1
br.select_form(nr=formcount)

I'm sure the loop counter method above could be done more pythonic, but this should select the form with attribute id="sblock".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...