Scraping websites with Xray
In this short post, we're going to scrape the website that this blog is hosted on to get all the links and posts back using Nodejs and Xray.
We are going to keep things super minimal and bare. We just want a proof of concept on how to scrape the data from the rendered website HTML.
Scraping the website
Going to the blog and inspecting with the Developer Tools, we can see that there isn't many classes to go with, but we can use the selectors to decide how we are going to get the information back.
The website with developer tools
Create a new file
index.js and add the following:
In the above script, we are simply running a main function that calls
getPosts and waits for the Promise to resolve before logging out the results.
The important part of the code comes from within the
x function is calling the blog URL, the looking for the last child of the
main DOM element you can see in the HTML DOM from the image shared above.
We are telling Xray to return an array of
items, and within that, we want to add all the elements that fit the object we pass. In our case, I am using standard selectors to grab the title, description and date, but am using the extra
@href helper with the
link to fetch the URL to the blog post!
That's it! Let's run the scraper now using
Perfect! Now you can take these same shorts tips and apply to anything you need to scrape down the track. Looking for alternatives or to use automation? You should also checkout Puppeteer or Playwright (added to resource links).
Resources and Further Reading
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.