Legendary_Hunter
Legendary_Hunter

Reputation: 1080

Beautiful Soup processing

I am trying to scrap the name and ratings of different travel agents from http://www.indiacom.com/yellow-pages/travel-agencies-and-services/ Here is my code

    from bs4 import BeautifulSoup
    import requests
    url="http://www.indiacom.com/yellow-pages/travel-agencies-and-services/"
    r=requests.get(url)
    soup=BeautifulSoup(r.content)
    links=soup.find_all("a")
    #for link in links:
     #   if"http" in link.get("href"):
      #      print("<a href='%s'>%s</a>"%(link.get("href"),link.text))
    L=[]
    g_data=soup.find_all("div",{"class": "Info_listing"})
    for item in g_data:
        L.append(item.contents[3].text)
       # L.append(item.text)
    for index in L:
        print(index)
    #print(L[2])

I am saving names and ratings in a list.Now I want to sort on the basis of ratings. How do i do that because if someone is rated their rating is displayed but if someone is not rated it comes "Be the first to rate" So how do I sort on the basis of rating

Upvotes: 2

Views: 129

Answers (1)

alecxe
alecxe

Reputation: 474151

Iterate over listings, construct a list of tuples containing listing names and ratings. The use sorted() to sort by rating value. Treat Be The First To Rate as a 0 rating:

from operator import itemgetter

listings = []
for item in soup.select("div.Details_listing"):
    name = item.a.text
    rating = item.find('div', id='total_ratings_details').text
    rating = 0 if rating.startswith('Be The First To Rate') else float(rating.split(' ')[0])

    listings.append((name, rating))

print sorted(listings, key=itemgetter(1))

Prints:

[
    (u'Jasvinder Tours And Travels', 0),
    ...
    (u'The Royal Tours & Travels', 2.9), 
    (u'Preeti Travels & Transport', 4.4)
]

Upvotes: 3

Related Questions