如何监控网站上不断更新的值?

我是Webscraping的新手,我不确定解决这个问题的最有效方法是什么。这个项目主要是用Python编写的,但我愿意为了提高效率而使用其他语言。

想象一下,网站上的一些js驱动的价值,比如实时访问者计数,或者一些金融资产的价格。加载网站后,随着新信息的出现而不断更新。我想要的是亚秒级采样一个这样的值,但自然,我不想用请求轰炸服务器,这无论如何都会破坏准确性。

我正在考虑一些事情,比如用硒或PyQt webkit加载一个网页,然后运行一个简单的循环来监控值,并在发生更改时更新数据库。我不是在寻找代码,我可以自己弄清楚,但我不确定这是否是解决这个问题的正确方法,或者是否有更有效的方法。


catspeake
浏览 96回答 3
3回答

暮色呼如

我已经完全按照你所指的做了,只使用Python和Selenium。我不明白为什么Python不会成为你特定用例使用的语言。我能看到的唯一问题是网页是否不断(或缓慢)变化。我遇到了一个问题,其中网页被重新设计,并通过了我的Xpath。我所要做的就是更正Xpath以反映新位置,它仍然有效。另一种解决方案可能是检查您尝试抓取的网页是否已经存在现有API。如果是这种情况,您可能不必进行任何网络抓取,只需调用API即可获得您的价值。这将是最有效的方法,但如果没有要调用的API,显然是不可能的。附注:我对Python最满意,并且在尝试自动执行任务时通常会默认使用它。可能有更多性能语言可以使用,但我的选择是Python。

达令说

这是潜在的破坏性操作,不适用于声明的任何内容(这会阻止重新分配)。您可以尝试在代理中“包装”需要监视的数据const在此示例中:该变量存在于页面上,并且是您要跟踪的变量。commodities该函数通过Selenium注入和执行。它保存一个小的“db”,用于记录任何更改。track_commodities

守着星空守着你

// inject that function and call it via Selenium >>>function track_commodities () {&nbsp; track_commodities.db = { gold: []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;, silver: []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;, copper: []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; commodities =&nbsp;&nbsp; &nbsp; new Proxy(commodities, {&nbsp; &nbsp; &nbsp; set: (o, k, v) => {&nbsp; &nbsp; &nbsp; &nbsp; track_commodities.db[k].push(v);&nbsp; &nbsp; &nbsp; &nbsp; o[k] = v;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}track_commodities();// <<<// Normal execution of your web page...commodities.gold = 20;commodities.gold = 30;commodities.gold = 40;commodities.gold++;commodities.gold++;console.log(commodities.gold);console.log(track_commodities.db.gold)<!-- This is the JS value you need to track --><script>let commodities = { gold: 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , silver: 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , copper: 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript