简体   繁体   中英

How to scrape links from a webpage using javascript?

I'm looking to scrape the links of post shown on facebook feed. I noticed that post link has two things in common it has https://www.facebook.com/username/posts/1234567890

https://www.facebook.com/ and /posts/ is always there.

I used this code to get all links on the page but I don't know how to only grab links with

https://www.facebook.com/ and /posts/ in this.

var links = document.querySelectorAll("a[href^='https://www.facebook.com']");

for(var i = 0; i< links.length; i++){
  console.log(links[i].href);
}

I tried regex and this is what I found after learning regex for this url pattern

^(https://www.|http://)[a-zA-Z0-9._$]+.[a-zA-Z]+/[a-zA-Z0-9]+/posts/[0-9]+$

but I don't know how to use this to get the result.

can anyone please help me with this?

Use getElementsByTagName , transform to Array, filter by your requirements, and map to get the URLs:

[...document.getElementsByTagName("A")]
.filter(link => 
  link.href.includes("https://www.facebook.com/") &&
  link.href.includes("/posts/")
)
.map(link => link.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