0

我正在尝试使用 Ghost.py 进行一些网络抓取。我正在尝试关注一个链接,但 Ghost 似乎并未真正评估 javascript 并关注该链接。我的问题是我在一个 HTTPS 会话中并且不能使用重定向。我还查看了其他选项(如 selenium),但我无法在将运行脚本的机器上安装浏览器。我还有一些 javascript 评估,所以我不能使用 mechanize。

这就是我要做的...

## 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()。

但是该页面确实正确加载了链接的网址。

欢迎任何想法。

查尔斯

4

1 回答 1

0

我认为您为此使用了错误的方法。
如果你想提交表单,有一个特殊的方法:

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)

您只需要为此找到正确的选择器。
我不知道您要在哪个页面上执行任务,因此我无法修复您的代码。但我希望这会对你有所帮助。

于 2014-04-27T11:52:51.550 回答