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