I am fairly new to using python. I want to create a web-scrpaing tool which allows me to scrape through 90 sites which are pre-collated in the form of an excel spreadsheet.
An example URL of what I'm trying to scrape is below: https://mycare.derby.gov.uk/Services/326
I have watched a few tutorials on how to use Beautifulsoup to achieve this and although these have been largely helpful in scraping some data, I am struggling to scrape the rest for two reasons:
Some of the data is in list form, specifically, I am trying to scrape 'CQC Service Type' and 'CQC Specialism' data but I can't work out how to code for this.
Some of the data is embedded into the website using a javascript window - specifically I want to scrape data on 'CQC overall rating' and 'Date of inspection'.
Here is my code so far:
import pandas as pd
import requests
from bs4 import BeautifulSoup
import http.client
http.client._MAXHEADERS = 1000
file="/Users/dhesis/PycharmProjects/pythonProject/CH_ID (2).xlsx"
Carehome_list = pd.read_excel(file).iloc[:, 0].to_list()
print(Carehome_list)
def loop_excel_sheet ():
for CH in Carehome_list[:2]:
url = CH
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
divs = soup.find_all('div', class_ ="container-fluid bem-js-service-details")
div_CQC = soup.find_all('dl', class_="ai-group-fields read-data bem-description-list bem-description-list--ai-group")
div_cqc_Rating = soup.find_all('span', class_="cqc-widget-overall requires-improvement" )
for item in divs:
name = item.find('dd', itemprop = 'branchOf').text
address = item.find('span', itemprop = 'location').text
try:
website = item.find('a', itemprop = 'url').text
except:
website = ''
last_updated = item.find('p').text.strip()
try:
telephone = item.find('dd', itemprop='telephone').text
except:
telephone = ''
info = {
'Name': name,
'Address': address,
'Website': website,
'Telephone': telephone,
'Website Last Updated': last_updated,
}
chlist.append(info)
chlist = []
loop_excel_sheet()
df= pd.DataFrame(chlist)
df.to_csv('CH Tester.csv')
#Remaining sections to be scraped
#CQC Specialism
#CQC service type
#CQC overall rating
#Date of CQC rating
Hopefully someone can help.