从Zillow获取价格图表数据

2024-05-15 03:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为自己做一个项目,寻找符合我标准的最好的房子。我使用Selenium(Chrome)从zillow下载了一段python代码。这将帮助我在Excel中加载数据并运行一些分析-我的使用是个人、时间和;位置有限且合法。以下是一个简短的描述和我的问题:

Desc: Zillow显示一些价格图表,如1。我需要这条曲线上的数据。它是通过如下所示的GraphQL脚本获取的(我是通过使用Firefox开发工具检查网站的网络活动获得的):

await fetch("https://www.zillow.com/graphql/?zpid=53084803&timePeriod=TEN_YEARS&metricType=LOCAL_HOME_VALUES&forecast=true&operationName=HomeValueChartDataQuery", { "credentials": "include", "headers": { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "*/*", "Accept-Language": "en-US,en;q=0.5", "content-type": "text/plain" }, "referrer": "https://www.zillow.com/homedetails/7317-Vineyard-Dr-Plano-TX-75025/53084803_zpid/", "body": "{\"query\":\"query HomeValueChartDataQuery($zpid: ID!, $metricType: HomeValueChartMetricType, $timePeriod: HomeValueChartTimePeriod) {\\n property(zpid: $zpid) {\\n homeValueChartData(metricType: $metricType, timePeriod: $timePeriod) {\\n points {\\n x\\n y\\n }\\n name\\n }\\n }\\n}\\n\",\"operationName\":\"HomeValueChartDataQuery\",\"variables\":{\"zpid\":53084803,\"timePeriod\":\"TEN_YEARS\",\"metricType\":\"LOCAL_HOME_VALUES\",\"forecast\":true},\"clientVersion\":\"home-details/6.0.11.0.0.hotfix-01-25-21.373d699\"}", "method": "POST", "mode": "cors" });

问题: 如果我在firefox或chrome的控制台中运行上述代码段,它将返回曲线信息。这在我的selenium chrome实例的控制台中也可以正常工作。现在我喜欢使用python运行它,所以我在我的selenium.webdriver实例上同时尝试了execute_scriptexecute_async_script。但是,我得到以下javascript错误:

JavascriptException: Message: javascript error: Unexpected identifier JavaScript stack: SyntaxError: Unexpected identifier at new Function (<anonymous>) at executeAsyncScript (<anonymous>:556:26) at <anonymous>:571:29 at callFunction (<anonymous>:450:22) at <anonymous>:464:23 at <anonymous>:465:3 (Session info: chrome=88.0.4324.96)

似乎错误来自selenium中chrome的Javascript解释器。我做了一些研究,似乎错误与await和承诺对象机制有关。然而,我对javascript不是很熟悉,因此我承诺。虽然我尝试了多种方法,但我不知道如何修复此错误

有人能解释我为什么会出现这个错误,以及我如何解决它吗?此外,有没有办法在python selenium中模拟chrome控制台(开发工具)

提前感谢,


Tags: 数据selenium错误javascriptchromefirefoxawait曲线

热门问题