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
642 views
in Technique[技术] by (71.8m points)

beautifulsoup - Using mechanicalsoup to set value of form element w/o a name

I have searched through all mechanicalsoup & beautifulsoup documentation but can't figure out how to set the value of a form element using 'id' (because it doesn't have a name).

import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()

browser.open(my_url)

form = browser.select_form('form[id="login-form"]')

browser.get_current_form().print_summary()
userid = browser.get_current_page().find('input', id='text-userid')
form.set("text-userid", "user")

This gets me -

<input class="login-text-box" id="text-userid" placeholder="Email" type="text" value=""/>
<input class="login-text-box" id="text-password" placeholder="Password" type="password" value=""/>
<input id="button-login" type="submit" value="Sign In"/>
<input id="remember-me-checkbox" name="rememberme" type="checkbox" value="rememberme"/>
LinkNotFoundError: No valid element named text-userid

I have tried referring to the element via id, CSS selector, and other combinations but get the same error. I am able to get the actual tag (which I found as a solution to a similar problem with submit buttons) with:

userid = browser.get_current_page().find('input', id='text-userid')

but form.set() doesn't accept tags directly.

Thanks for any help!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

After more research, I learned I should be using Selenium for this case (not mechanicalsoup). I used this guide which did exactly what I needed on the first try.

http://stanford.edu/~mgorkove/cgi-bin/rpython_tutorials/Scraping_a_Webpage_Rendered_by_Javascript_Using_Python.php


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

...