我试着用Ghost.py做一些网络抓取。我试图跟踪一个链接,但是Ghost似乎并没有对javascript和链接进行实际评估。我的问题是,我处于HTTPS会话中,不能使用重定向。我也看过其他选项(比如selenium),但我不能在运行脚本的机器上安装浏览器。我也有一些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。
任何想法都欢迎。
查尔斯
发布于 2014-04-27 11:52:51
我认为你在这方面使用了错误的方法。
如果您想提交表单,有一种特殊的方法:
page, resources = ghost.fire_on("loginform", "submit", expect_loading=True)此外,还有一个特殊的ghost.py方法用于执行单击:
ghost.click('#some-selector')如果您只想打开该链接,另一种可能是:
link_url = ghost.evaluate("document.links[4]")[0]
ghost.open(link_url)你只需要找到正确的选择器就可以了。
我不知道您想在哪个页面上执行任务,因此我无法修复您的代码。但我希望这能帮到你。
https://stackoverflow.com/questions/21685709
复制相似问题