【发布时间】:2021-10-05 05:09:26
【问题描述】:
我使用 Cheeriogs 库进行抓取:
https://github.com/tani/cheeriogs
这是我需要收集值href的元素:
<a class="tnmscn" itemprop="url" href="/en/predictions-tips-wealdstone-solihull-moors-1455115">
这是我目前用来提取值的代码。:
const contentText = UrlFetchApp.fetch(url).getContentText();
const $ = Cheerio.load(contentText);
const scrapurl = $('div.schema > div > div.tnms > div > a.tnmscn');
const urlmatch = $(scrapurl).attr('href').trim();
Logger.log(urlmatch);
但这并不可靠,因为我担心最终会改变网站上的位置并收集除该位置的可点击元素中出现的链接之外的其他链接:
所以我想让它更安全,所以我尝试使用:
div.schema > div > div.tnms > div > a:contains("/en/predictions-tips")
那没用。我应该如何使用contains 来满足这个需求?
添加信息:
页面链接
https://www.forebet.com/en/teams/wealdstone
图像到元素
【问题讨论】:
-
你能澄清你的问题吗?您当前的 css 不可靠怎么办?你能告诉我们你期待什么输出吗?我看到有两个节点符合你的规则,你想要这两个值吗?
'/en/predictions-tips-wealdstone-solihull-moors-1455115', '/en/predictions-tips-northampton-walsall-fc-1474257'?你试过简单的a.tnmscn::attr(href)吗? -
非常感谢您提出疑问,我添加了更多有关所需值位置的详细信息。 @Granitosaurus
标签: google-apps-script web-scraping cheerio