mechanize(python)点击一个javascript类型的链接 [英] mechanize (python) click on a javascript type link

查看:127
本文介绍了mechanize(python)点击一个javascript类型的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让机械化遵循javascript类型的锚链接?

is it possible to have mechanize follow an anchor link that is of type javascript?

我正在尝试使用mechanize和beautifulsoup登录python中的网站。

I am trying to login into a website in python using mechanize and beautifulsoup.

这是锚链接

<a id="StaticModuleID15_ctl00_SkinLogin1_Login1_Login1_LoginButton" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;StaticModuleID15$ctl00$SkinLogin1$Login1$Login1$LoginButton&quot;, &quot;&quot;, true, &quot;Login1&quot;, &quot;&quot;, 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:

urllib2.URLError:

感谢任何帮助或建议

编辑

在帮助者的评论之后,我去看了一下asp页面的代码。

After the comment by helpers, I went and looked at the code of the asp page a bit.

我发现了一点有点有用的脚本,但我不确定我在python中必须做什么来使用python模拟JS代码。
我在哪里看到没有设置任何cookie,我看错了地方吗?

I found a little bit of useful scripts but I am unsure of what I have to do in python to emulate the JS code with python. In no where did I see any cookies set, am I looking at the wrong places?

<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。

I don't think this is possible with the mechanize module: it doesn't have the ability to interact with JavaScript: its purely Python and HTTP based.

那就是说,你可能会在 python-spidermonkey 模块中进行交互,它似乎旨在让你做的就是这种事。根据它的网站,它的目的是让你

That said, you may be intested in python-spidermonkey module, which it seems is aimed at letting you do just this kind of thing. According to it's website it's aim is to let you


从Python执行任意JavaScript代码。允许你引用任意Python对象和函数JavaScript VM

"Execute arbitrary JavaScript code from Python. Allows you to reference arbitrary Python objects and functions in the JavaScript VM"

我还没有使用它,但它看起来确实会像你想要的那样,尽管它仍然是阿尔法。

I've not used it yet but it certainly looks like it would do what you are looking for, although it is still in alpha.

这篇关于mechanize(python)点击一个javascript类型的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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