使用MechanicalSoup设置不带名称的表单元素的值 [英] Using mechanicalsoup to set value of form element w/o a name

查看:151
本文介绍了使用MechanicalSoup设置不带名称的表单元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了所有的机械汤和beautifulsoup文档,但无法弄清楚如何使用'id'设置表单元素的值(因为它没有名称).

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

这让我-

<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

我尝试通过id,CSS选择器和其他组合引用元素,但得到相同的错误.我可以通过以下方式获取实际标签(我发现它是解决与提交按钮类似问题的一种解决方案):

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')

但是form.set()不直接接受标签.

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

感谢您的帮助!

推荐答案

经过更多研究,我了解到在这种情况下我应该使用Selenium(而不是Mechanicalsoup).我使用了本指南,该指南完全满足了我第一次尝试时的需要.

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

这篇关于使用MechanicalSoup设置不带名称的表单元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆