简体   繁体   中英

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/

I just need to run the script in the DevTools console and get the 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

I only need the urls of RSS Feed . How can I console.log the first anchor or selects[i] (on each loop).

Following doesn't seem to be perfect but provides fairly consistent 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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