【发布时间】:2015-02-27 13:33:17
【问题描述】:
试图从http://metservice.com/maps-radar/local-observations/local-3-hourly-observations获取天气数据
确实找到了示例 here 关于如何使用 Ghost 抓取网页动态内容,但我还没有找到如何处理结果。
由于 ghost 在我使用的交互式 shell 中运行时似乎有问题
打印(结果)
通过管道输出到文件:
python getMetObservation.py >proper_result
这是我的python代码:
从幽灵导入幽灵
网址 = 'http://metservice.com/maps-radar/local-observations/local-3-hourly-observations'
gh = Ghost(wait_timeout=60)
页面,资源 = gh.open(url)
结果,资源 = gh.evaluate("document.getElementsByClassName('obs-content');")
打印(结果)
在检查文件时,它确实包含我想要的内容,但它也包含大量我不想要的信息。 也不清楚如何使用评估返回的变量结果。 检查 ghost.py 似乎是由
self.main_frame.evaluateJavaScript("%s" % script)
在:
def 评估(自我,脚本):
"""评估页面框架中的脚本。:param script:要评估的脚本。
"""
返回 (
self.main_frame.evaluateJavaScript("%s" % script),
self._release_last_resources(),
)
当我执行命令时:
document.getElementsByClassName('obs-content');
在 Chromium 控制台中,我得到了正确的响应。
我是 python 的初学者,但愿意学习。 另请注意,如果重要的话,我会在 Ubuntu 下的 python 虚拟环境中运行它。
【问题讨论】:
标签: python dynamic web-scraping ghost.py