-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebscraper.py
More file actions
81 lines (49 loc) · 2.29 KB
/
Copy pathwebscraper.py
File metadata and controls
81 lines (49 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
###########################################################################
# Script Name: webscraper.py
# Create Date: 12/05/2018
# Description: The purpose of this file is to enter a website url and perform a
# simple scrape of the webpage's content and parse through the text objects using
# regular expressions.
# Author: Mr. Machine
# Tags: python, utility, web, url
# Resources: https://towardsdatascience.com/web-scraping-regular-expressions-and-data-visualization-doing-it-all-in-python-37a1aade7924
###########################################################################
# Library imports
import requests
import re
from bs4 import BeautifulSoup
# Define the desired URL to scrape
# url = http://www.cleveland.com/metro/index.ssf/2017/12/case_western_reserve_university_president_barbara_snyders_base_salary_and_bonus_pay_tops_among_private_colleges_in_ohio.html # Example web URL for scraping demo
web_url = input("Enter the complete URL to scrape: ")
print("You entered " + web_url)
confirm = input("Is this entry correct? (Y/n)")
if confirm == Y or y:
pass
else:
break
# Make the GET request to the defined URL of website
# r = requests.get(url) # Example GET request for demo
r = requests.get(web_url)
# Extract the content
c = r.content
# Create soup object
soup = BeautifulSoup(c)
# Find the desired element on the webpage
# main_content = soup.find('div', attrs = {'class': 'entry-content'}) #Example entries for main content html tag
main_content = soup.find()
# Extract the relevant content as text
# content = main_content.find('ul').text # Example html 'ul' tag to search unordered lists within main content
content = main_content.find().text
# Create a pattern to match names
name_pattern = re.compile(r'^([A-Z]{1}.+?)(?:,)', flags = re.M)
# Find all occurrences of the pattern
names = name_pattern.findall(content)
# FMake school pattern nd extract schools
school_pattern = re.compile(r'(?:,|,\s)([A-Z]{1}.*?)(?:\s\(|:|,)')
schools = school_pattern.findall(content)
# Pattern to match the salaries
salary_pattern = re.compile(r'\$.+')
salaries = salary_pattern.findall(content)
# Convert salaries to numbers in a list comprehension
[int(''.join(s[1:].split(','))) for s in salaries]