From here on, we will dig deeper to get the specific elements containing the price, image type, and rating information.
After the previously presented lines of code, copy the following:
const results = []
places.forEach(place => {
if (place) {
const price = place.querySelector('._ls0e43')
if (price) place.price = price.querySelector('._krjbj').innerHTML
const image = place.querySelector('._91slf2a')
if (image) place.image = image.src
const type = place.querySelector('._b14dlit')
if (type) place.type = type.innerHTML
const rating = place.querySelector('._10fy1f8')
if (rating) place.rating = rating.innerHTML
results.push(place)
}
})
console.log(results)
As you can see, for each listing we get on the first page, we fetch the price tag element, the image source location, the type of the listing, and the rating. In the end, we will have an array of objects, and each of them will contain every element in this list.
Now that we have written all the code necessary to scrape the Airbnb information, the index.js file should look something like this:
const {JSDOM} = require("jsdom");
const got = require("got");
(async () => {
const params = {
api_key: "YOUR_API_KEY",
url: "https://www.airbnb.com/s/Berlin/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&flexible_trip_dates%5B%5D=april&flexible_trip_dates%5B%5D=may&flexible_trip_lengths%5B%5D=weekend_trip&date_picker_type=calendar&source=structured_search_input_header&search_type=filter_change&place_id=ChIJAVkDPzdOqEcRcDteW0YgIQQ&checkin=2021-04-01&checkout=2021-04-08"
}
const response = await got('https://api.webscrapingapi.com/v1', {searchParams: params})
const {document} = new JSDOM(response.body).window
const places = document.querySelectorAll('._gig1e7')
const results = []
places.forEach(place => {
if (place) {
const price = place.querySelector('._ls0e43')
if (price) place.price = price.querySelector('._krjbj').innerHTML
const image = place.querySelector('._91slf2a')
if (image) place.image = image.src
const type = place.querySelector('._b14dlit')
if (type) place.type = type.innerHTML
const rating = place.querySelector('._10fy1f8')
if (rating) place.rating = rating.innerHTML
results.push(place)
}
})
console.log(results)
})()
As you can see, scraping Airbnb data using WebScrapingAPI it’s pretty simple.
- Make a request to WebScrapingAPI using the necessary parameters: the API key and the URL we need to scrape data from.
- Load the DOM using JSDOM.
- Select all the listings by finding the specific class.
- For each listing, get the price tag, image, listing type, and rating.
- Add every place to a new array called results.
- Log the newly created results array to the screen.
The response should look something like this:
[
HTMLDivElement {
price: '$47 per night, originally $67',
image: 'https://a0.muscache.com/im/pictures/miso/Hosting-46812239/original/c56d6bb5-3c2f-4374-ac01-ca84a50d31cc.jpeg?im_w=720',
type: 'Room in serviced apartment in Friedrichshain',
rating: '4.73'
},
HTMLDivElement {
price: '$82 per night, originally $109',
image: 'https://a0.muscache.com/im/pictures/miso/Hosting-45475252/original/f6bd7cc6-f72a-43ef-943e-deba27f8253d.jpeg?im_w=720',
type: 'Entire serviced apartment in Mitte',
rating: '4.80'
},
HTMLDivElement {
price: '$97 per night, originally $113',
image: 'https://a0.muscache.com/im/pictures/92966859/7deb381e_original.jpg?im_w=720',
type: 'Entire apartment in Mitte',
rating: '4.92'
},
HTMLDivElement {
price: '$99 per night, originally $131',
image: 'https://a0.muscache.com/im/pictures/f1b953ca-5e8a-4fcd-a224-231e6a92e643.jpg?im_w=720',
type: 'Entire apartment in Prenzlauer Berg',
rating: '4.90'
},
HTMLDivElement {
price: '$56 per night, originally $61',
image: 'https://a0.muscache.com/im/pictures/bb0813a6-e9fe-4f0a-81a8-161440085317.jpg?im_w=720',
type: 'Entire apartment in Tiergarten',
rating: '4.67'
},
...
]
One of the limitations that we are currently facing is that we only scrape the information from one page of our search. This can be fixed by using some type of headless browser, like Puppeteer, or a browser automation tool like Selenium. This will help us do most of the things we can manually do in a web browser, like completing a form or clicking a button.
We encourage you to check out our Ultimate Guide to Web Scraping with JavaScript and Node.Js if you want to find out more about these technologies.