简体   繁体   English

获取每个元素的第一个锚点的 href 值

[英]get href value of the first anchor for each element

I am trying to extract the links of the rss feeds froms https://blog.feedspot.com/hr_rss_feeds/我正在尝试从https://blog.feedspot.com/hr_rss_feeds/中提取 rss 提要的链接

I just need to run the script in the DevTools console and get the output.我只需要在 DevTools 控制台中运行脚本并获取 output。

The script I came up with is我想出的脚本是

var selects=document.querySelectorAll("p.trow.trow-wrap a");
for (i = 0; i < selects.length; ++i) {

      if (selects[i].getAttribute("rel") === "noopener nofollow")
      {
        console.log (selects[i].item(1).href);
      }
}

The problem is, it gets the url of both RSS Feed & Site问题是,它获得了RSS FeedSite的 url

I only need the urls of RSS Feed .我只需要RSS Feed的网址。 How can I console.log the first anchor or selects[i] (on each loop).我如何console.log第一个锚点或selects[i] (在每个循环上)。

Following doesn't seem to be perfect but provides fairly consistent output以下似乎并不完美,但提供了相当一致的 output

const feedLinks = [...document.querySelectorAll("p.trow.trow-wrap")].map(el=>{
  return el.querySelector('a').href
})
console.log(feedLinks)

OP here, the code that is working for me is:在这里,对我有用的代码是:

var selects=document.querySelectorAll("p.trow.trow-wrap");
for (i = 0; i < selects.length; ++i) {
        console.log (selects[i].querySelectorAll("a")[0].href);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM