使用Cheerio库从网页中提取数据
我正在尝试使用 Cheerio 和Google Apps脚本。 我想从此 webpage :
以下是我用来获取它的代码段:
function LinkResult(){
var url ='https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.juicecoldpressed.com%2F';
var result = UrlFetchApp.fetch(url);
var content = Cheerio.load(result.getContentText())
var item = content(".lh-gauge__percentage").text()
Logger.log(item)
}
当我运行时,此代码不会在变量中显示任何输出项目
。当然有我缺少的东西,你能指导我吗?谢谢。
I am trying to scrape very small information from a webpage using Cheerio and Google Apps Script.
I want to get the performance number from this webpage:
Following is the code snippet that I am using to get it:
function LinkResult(){
var url ='https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.juicecoldpressed.com%2F';
var result = UrlFetchApp.fetch(url);
var content = Cheerio.load(result.getContentText())
var item = content(".lh-gauge__percentage").text()
Logger.log(item)
}
As I run, this code does not show any output in the variable item
. Surely there is something which I am missing, can you please guide me? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题和解决方法:
在这种情况下,我担心您的目标可能无法使用
https://pagespeed.web.dev/report?url=http%3A%2F%直接实现。 2fwww.juicecoldpressed.com%2F
和Cheerio
。因为从urlfetchapp.fetch(url)
检索的HTML数据与浏览器上的数据不同。而且,似乎该值是使用脚本计算的。幸运的是,在您的情况下,我认为可以使用PagesPeed Insights API检索您的价值。在此答案中,我想建议使用PagesPeed Insights API实现您的目标。
用法:
1。开始使用PagesPeed Insights API。
请检查官方文档用于使用PagesPeed Insights api。在这种情况下,需要使用API密钥。并且,请在API控制台启用PagesPeed Insights API。
2。示例脚本。
3。测试。
运行此脚本后,您可以在日志中查看
{桌面:##,移动:##}
的返回值。desktop
和Mobile
的值分别是桌面和移动设备的值。参考:
Issue and workaround:
In this case, I'm worried that your goal might not be able to be directly achieved using the URL of
https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.juicecoldpressed.com%2F
andCheerio
. Because the HTML data retrieved fromUrlFetchApp.fetch(url)
is different from that on the browser. And, it seems that the value is calculated using a script.Fortunately, in your situation, I thought that your values can be retrieved using PageSpeed Insights API. In this answer, I would like to propose achieving your goal using PageSpeed Insights API.
Usage:
1. Get Started with the PageSpeed Insights API.
Please check the official document for using PageSpeed Insights API. In this case, it is required to use your API key. And, please enable PageSpeed Insights API at the API console.
2. Sample script.
3. Testing.
When this script is run, you can see the returned value of
{ desktop: ##, mobile: ## }
at the log. The values (the unit is%
.) ofdesktop
andmobile
are the values for the desktop and the mobile, respectively.Reference: