Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 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 Answer

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...