首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ghost.py链接

使用Ghost.py链接
EN

Stack Overflow用户
提问于 2014-02-10 19:04:16
回答 1查看 2K关注 0票数 0

我试着用Ghost.py做一些网络抓取。我试图跟踪一个链接,但是Ghost似乎并没有对javascript和链接进行实际评估。我的问题是,我处于HTTPS会话中,不能使用重定向。我也看过其他选项(比如selenium),但我不能在运行脚本的机器上安装浏览器。我也有一些javascript的进一步评估,所以我不能使用机械化。

我就是这么做的..。

代码语言:javascript
复制
## Open the website
page,resources = ghost.open('https://my.url.com/')

## Fill textboxes of the form (the form didn't have a name)
result, resources = ghost.set_field_value("input[name=UserName]", "myUser")
result, resources = ghost.set_field_value("input[name=Password]", "myPass")

## Submitting the form
result, resources = ghost.evaluate( "document.getElementsByClassName('loginform')[0].submit();", expect_loading=True)

## Print the link to make sure that's the one I want to follow
#result, resources = ghost.evaluate( "document.links[4].href")

## Click the link
result, resources = ghost.evaluate( "document.links[4].click()")

#print ghost.content

当我查看ghost.content时,我仍然在同一个页面上,结果是空的。我注意到,当我在尝试计算单击时添加expect_loading=True时,我会得到一个超时错误。

当我尝试在Chrome开发工具控制台中运行javascript时,我得到

event.returnValue被否决了。请改用标准的event.preventDefault()。

但是页面确实正确地加载了链接的url。

任何想法都欢迎。

查尔斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-27 11:52:51

我认为你在这方面使用了错误的方法。

如果您想提交表单,有一种特殊的方法:

代码语言:javascript
复制
page, resources = ghost.fire_on("loginform", "submit", expect_loading=True)

此外,还有一个特殊的ghost.py方法用于执行单击:

代码语言:javascript
复制
ghost.click('#some-selector')

如果您只想打开该链接,另一种可能是:

代码语言:javascript
复制
link_url = ghost.evaluate("document.links[4]")[0]
ghost.open(link_url)

你只需要找到正确的选择器就可以了。

我不知道您想在哪个页面上执行任务,因此我无法修复您的代码。但我希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21685709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档