机械化(python)单击javascript类型链接
作者:互联网
是否可以机械化遵循javascript类型的锚链接?
我正在尝试使用机械化和beautifulsoup登录python中的网站.
这是锚链接
<a id="StaticModuleID15_ctl00_SkinLogin1_Login1_Login1_LoginButton" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("StaticModuleID15$ctl00$SkinLogin1$Login1$Login1$LoginButton", "", true, "Login1", "", false, true))"><img id="StaticModuleID15_ctl00_SkinLogin1_Login1_Login1_Image2" border="0" src="../../App_Themes/default/images/Member/btn_loginenter.gif" align="absmiddle" style="border-width:0px;" /></a>
这是我尝试过的
links = SoupStrainer('a', id="StaticModuleID15_ctl00_SkinLogin1_Login1_Login1_LoginButton")
[anchor for anchor in BeautifulSoup(data, parseOnlyThese=links)]
link = mechanize.Link( base_url = self.url,
url = str(anchor['href']),
text = str(anchor.string),
tag = str(anchor.name),
attrs = [(str(name), str(value))
for name, value in anchor.attrs])
response2 = br.follow_link(link)
现在,我收到以下错误消息:
urllib2.URLError:
任何帮助或建议表示赞赏
编辑
在助手的评论之后,我去了一下asp页面的代码.
我发现了一些有用的脚本,但是不确定用python模拟JS代码时必须在python中做什么.
我在哪里都看不到任何cookie设置,是在看错地方吗?
<form name="form1" method="post" action="BrowseSchedule.aspx?ItemId=75" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
function WebForm_DoPostBackWithOptions(options) {
var validationResult = true;
if (options.validation) {
if (typeof(Page_ClientValidate) == 'function') {
validationResult = Page_ClientValidate(options.validationGroup);
}
}
if (validationResult) {
if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0)) {
theForm.action = options.actionUrl;
}
if (options.trackFocus) {
var lastFocus = theForm.elements["__LASTFOCUS"];
if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {
if (typeof(document.activeElement) == "undefined") {
lastFocus.value = options.eventTarget;
}
else {
var active = document.activeElement;
if ((typeof(active) != "undefined") && (active != null)) {
if ((typeof(active.id) != "undefined") && (active.id != null) && (active.id.length > 0)) {
lastFocus.value = active.id;
}
else if (typeof(active.name) != "undefined") {
lastFocus.value = active.name;
}
}
}
}
}
}
if (options.clientSubmit) {
__doPostBack(options.eventTarget, options.eventArgument);
}
}
解决方法:
我认为使用机械化模块不可能做到这一点:它没有与JavaScript交互的能力:它纯基于Python和HTTP.
就是说,您可能已经在python-spidermonkey模块中接受了测试,似乎旨在让您执行这种操作.根据它的网站,目的是让您
“Execute arbitrary JavaScript code from Python. Allows you to reference arbitrary Python objects and functions in the JavaScript VM”
我还没有使用过它,但是尽管它仍然处于alpha状态,但它看起来确实可以满足您的需求.
标签:mechanize,javascript,python 来源: https://codeday.me/bug/20191107/2003170.html