【问题标题】:Follow a link with Ghost.py跟随 Ghost.py 的链接
【发布时间】:2014-02-10 19:04:16
【问题描述】:

我正在尝试使用 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() 代替。

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

欢迎提出任何想法。

查尔斯

【问题讨论】:

    标签: python ghost.py


    【解决方案1】:

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

    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)
    

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

    【讨论】:

    • 感谢回复,我明天应该可以试试了。
    • 我从您在表单提交中提出的替代方案中收到 TimeoutError。无论如何,我尝试了 [code]ghost.click('a[href="/Internet/Home"]')[/code] 这导致我同样... Ghost浏览器不跟随链接并继续同一页。
    • 我从 [code]ghost.open(link_url['href'])[/code] 得到了同样的结果检索一些带宽统计...
    • 嗯,这很奇怪。但是您是对的,发布您的凭据不是最好的主意;)。我只是认为您打开“my.url.com”的网址是一些公共网站。如果您可以查看其源代码,那将会有所帮助。也许表单字段没有正确填写。您是否尝试通过启用图形输出来查看 ghost 正在做什么?您可以使用以下命令打开它:Ghost(display=True)。它显示什么?
    • 我不知道你可以显示浏览器,很高兴知道。
    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多