How to scrape links from a webpage using javascript?

梦想与她 提交于 2021-01-29 22:17:25

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/64517282/how-to-scrape-links-from-a-webpage-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!