Prompt Detail:
To scrape data from Justdial using Python, you can follow these steps:
- Install the necessary libraries:
- BeautifulSoup: Used for parsing HTML data
- requests: Used to send HTTP requests
You can install these libraries using pip:
pythonpip install beautifulsoup4 pip install requests
- Import the required libraries in your Python script:
pythonimport requests
from bs4 import BeautifulSoup
- Send a GET request to the Justdial website's search page:
pythondef scrape_justdial_data():
url = 'https://www.justdial.com/Mumbai/Restaurants'
response = requests.get(url)
if response.status_code == 200:
return response.content
else:
return None
- Parse the HTML content using BeautifulSoup:
pythondef scrape_justdial_data():
# ...
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
return soup
else:
return None
- Find and extract the relevant data from the parsed HTML:
pythondef scrape_justdial_data():
# ...
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
# Find the container elements that hold the desired data
containers = soup.find_all('div', class_='col-sm-5 col-xs-8 store-details sp-detail paddingR0')
for container in containers:
# Extract specific data from each container
name = container.find('span', class_='lng_cont_name').text.strip()
phone = container.find('span', class_='mobilesv').text.strip()
address = container.find('span', class_='cont_fl_addr').text.strip()
# Print or store the extracted data as needed
print('Name:', name)
print('Phone:', phone)
print('Address:', address)
print('---')
else:
return None
Please note that web scraping should be done responsibly and in compliance with the website's terms of service. Make sure to read and understand the website's terms of use before scraping their data. Additionally, consider incorporating delays in your code to avoid overwhelming the website's server with too many requests.
Add a comment