Reputation: 55
I'm trying to scrape the price for a list of courses on this website.
However, I'm having trouble finding the page where I can see the entire list of courses and their prices.
I was able to come up with below code, which pulls the price for a single course:
import pandas as pd
import requests
url = "https://www.learningconnection.philips.com/en/course/pinnacle%C2%B3-advanced-planning-education"
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
price = soup.select_one("[class*='field-price'] .even").text
print(price)
Any help/suggestions appreciated!
Upvotes: 0
Views: 60
Reputation: 84465
Here is one way of looping down through that area of interest. Uses bs4 4.7.1 + is order to have access to :contains
import requests
from bs4 import BeautifulSoup as bs
base = 'https://www.learningconnection.philips.com'
url = f'{base}/en/catalog/profession/biomedical-engineers'
courses = []
results = []
with requests.Session() as s:
r = s.get(url)
soup = bs(r.content, 'lxml')
links = [base + i['href'] for i in soup.select('h3 a')]
for link in links:
r = s.get(link)
soup = bs(r.content, 'lxml')
courses+=[i['href'] for i in soup.select('.title a')]
for course in courses:
r = s.get(course)
soup = bs(r.content, 'lxml')
price = soup.select_one('em:contains("Tuition:")')
if price is None:
price = 'Not listed'
else:
price = price.text.replace('\xa0',' ')
result = {'Title':soup.select_one('#page-title').text.replace('\xa0',' ')
,'Description': soup.select_one('.field-item p').text.replace('\xa0',' ')
,'Price': price
, 'Url':course}
results.append(result)
print(results)
Upvotes: 1
Reputation: 33384
Use the Key-Word to search the item and then based on that search results get all the urls. Once you get the urls loop it.
from bs4 import BeautifulSoup
import requests
Search_key='pinnacle'
url = "https://www.learningconnection.philips.com/en/search/site/{}".format(Search_key)
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
urls=[item['href'] for item in soup.select('h3.title > a')]
price=[]
for url in urls:
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
if soup.select_one("[class*='field-price'] .even"):
price.append(soup.select_one("[class*='field-price'] .even").text)
print(price)
Output:
['5171.00', '5171.00', '3292.00', '5141.00', '4309.00', '2130.00', '2130.00', '2130.00']
You can also print the course title.
from bs4 import BeautifulSoup
import requests
Search_key='pinnacle'
url = "https://www.learningconnection.philips.com/en/search/site/{}".format(Search_key)
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
urls=[item['href'] for item in soup.select('h3.title > a')]
price=[]
title=[]
for url in urls:
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
if soup.select_one("[class*='field-price'] .even"):
title.append(soup.select_one("h1#page-title").text)
price.append(soup.select_one("[class*='field-price'] .even").text)
print(title)
print(price)
Output:
['Pinnacle³ Auto Segmentation with SPICE', 'Pinnacle³ Dynamic Planning', 'Pinnacle³ Additional Education', 'Pinnacle³ Advanced Planning Education', 'Pinnacle³ Basic Planning Education', 'Pinnacle³ Physics Modeling', 'Pinnacle³ Level I Basic Planning Education', 'Pinnacle³ Level II Education']
['5171.00', '5171.00', '3292.00', '5141.00', '4309.00', '2130.00', '2130.00', '2130.00']
Edited
from bs4 import BeautifulSoup
import requests
Search_key='biomed'
url = "https://www.learningconnection.philips.com/en/search/site/{}".format(Search_key)
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
urls=[item['href'] for item in soup.select('h3.title > a')]
print(len(urls))
price=[]
title=[]
for url in urls:
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
if soup.select_one("[class*='field-price'] .even"):
title.append(soup.select_one("h1#page-title").text)
price.append(soup.select_one("[class*='field-price'] .even").text)
print(title)
print(price)
Output:
28
['NETWORK CONCEPTS (BIOMED)']
['4875.00']
Upvotes: 0
Reputation: 71471
You can find the price by anchoring your search on the item's parent wrapper:
import requests
from bs4 import BeautifulSoup as soup
d = soup(requests.get('https://www.learningconnection.philips.com/en/course/pinnacle%C2%B3-advanced-planning-education').text, 'html.parser')
prices = [i.find_all('div', {'class':'field-item even'})[2].text for i in d.find_all('fieldset', {'class':' group-overview field-group-fieldset panel panel-default form-wrapper'})]
Output:
['5141.00']
Upvotes: 0