Debug School

rakesh kumar
rakesh kumar

Posted on

How to get image url using Beautiful soup library

how to get all image url and store in list
how to remove ₹ in all element of list using replace command and store in list

Write a python program to scrape mentioned details from dineout.co.inand make data framei) Restaurant name
ii) Cuisine
iii) Location
iv) Ratings
v) Image URL

Image description

step 1 first install libraries

pip install bs4
pip install request
Enter fullscreen mode Exit fullscreen mode

step 2 import libraries

from bs4 import BeautifulSoup
import requests
Enter fullscreen mode Exit fullscreen mode

step3:Send an HTTP GET request to the URL and (status code 200)

page  = requests.get('https://www.dineout.co.in/delhi-restaurants')
page
Enter fullscreen mode Exit fullscreen mode

output

<Response [200]>
Enter fullscreen mode Exit fullscreen mode

step4: check page content

soup= BeautifulSoup(page.content)
soup
Enter fullscreen mode Exit fullscreen mode

output

Image description

step5: get all the element of specific class

data = soup.find_all('div', class_="restnt-info cursor")
data
Enter fullscreen mode Exit fullscreen mode

output

Image description

step6: get all restaurant title of specific class and store in list

restaurant_title=[]
for i in data:    
    restaurant_title.append(i.text)
restaurant_title
Enter fullscreen mode Exit fullscreen mode

output
Image description

step7: get all restaurant location of specific class and store in list

restaurant_location=[]

for i in soup.find_all('div', class_="restnt-loc ellipsis"):    
    restaurant_location.append(i.text)
restaurant_location
Enter fullscreen mode Exit fullscreen mode

output

Image description

step7: get all restaurant price of specific class and store in list
how to remove ₹ in all element of list using replace command and store in list

restaurant_price=[]

for i in soup.find_all('span', class_="double-line-ellipsis"):    
    restaurant_price.append(i.text.replace('₹',''))
restaurant_price
Enter fullscreen mode Exit fullscreen mode

output

Image description

step7: get all restaurant image url of specific class and store in list

**how to get all image url and store in list**
restaurant_images=[]
for i in soup.find_all('img', class_="no-img"):    
    restaurant_images.append(i['data-src'])
restaurant_images
Enter fullscreen mode Exit fullscreen mode

output

Image description

import pandas as pd
df= pd.DataFrame({'title':restaurant_title,'location':restaurant_location,'price':restaurant_price,'image':restaurant_images})
df
Enter fullscreen mode Exit fullscreen mode

output

Image description

Top comments (0)