ranking of a host in Airbnb for a specific city or location

Viewed 72

I want to get the ranking of an host from Airbnb site by scraping the data from the pages with the individual reviews and ranking in the region. I am able to get the number of review from the page but does not have any idea about the ranking. Following is my code

from bs4 import BeautifulSoup # BeautifulSoup is in bs4 package 
import requests

URL = input("Please enter an URL to the number of reviews \n")
#'https://www.airbnb.co.in/users/show/154131896'
content = requests.get(URL)

soup = BeautifulSoup(content.text, 'html.parser')

row = soup.find('div') 
#Extract and return first occurrence          
print("=========Text Result==========")
print(row.get_text())
#Print row as text

divs = soup.find_all('div', { "class" : "_5kaapu"})
#Find all the records  for reviews
for i in divs:

childr = i.findChildren("span" , recursive=False)
for child in childr:
    iwant = child.text
    print(iwant)

contentTable  = soup.find('div', { "class" : "_5kaapu"}) 
# Use dictionary to pass key : value pair
rows  = contentTable.find_all('span',{"class": "_1ax9t0a"})
for row in rows:
     print(" The host have ", row.get_text()) 
2 Answers

One way of doing this would be:

from bs4 import BeautifulSoup
import requests

content = requests.get('https://www.airbnb.co.in/users/show/154131896')

soup = BeautifulSoup(content.text, 'html.parser').find_all('span', {"class": "_krjbj"})

for item in soup[:2]:
    print(item.text)

This prints out:

Rating 4.97 out of 5;
36 reviews

High chances that the class names will rotate to avoid scraping. You may try an approach using some regex logic to identify the good tags :

import re
from bs4 import BeautifulSoup
import requests

response = requests.get('https://www.airbnb.co.in/users/show/154131896')
soup = BeautifulSoup(response.content, 'html.parser')

#Regex pattern to find the tag containing the rating
pattern = r'Rating (.*?) out of'

rating_tag = soup.find('span',text = re.compile(pattern))
rating_value = float(re.findall(pattern, rating_tag.text)[0])

#Finding the tag containing the reviews count
reviews_tag = rating_tag.findNext('span', text= re.compile(' review'))
reviews_value = int(re.findall(r'\d+',reviews_tag.text)[0])

print(f'User rating is: {rating_value} over {reviews_value} votes')

Output:

User rating is: 4.97 over 36 votes
Related