-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_list_scraper.py
More file actions
57 lines (47 loc) · 1.74 KB
/
web_list_scraper.py
File metadata and controls
57 lines (47 loc) · 1.74 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
#This script provides functions for scraping text data from lists on websites
try:
import requests
import bs4
import csv
except:
print("There was an error importing the necessary libraries.")
#BeautifulSoup objects are strings containing HTML tags and content, referred
#to as 'soup' throughout the script
def get_soup(url):
"""Returns soup from webpage
str -> soup"""
response = requests.get(url)
soup = bs4.BeautifulSoup(response.text)
return soup
def get_lists(soup,html_class=''):
"""Returns list of soup elements with 'ul' tag and given class
soup, str -> list of soups"""
if html_class == '':
return soup.find_all('ul')
else:
return soup.find_all('ul', html_class)
def list_items(soup):
"""Returns a list of strings with items from unordered list
soup -> list of str"""
contents = str(soup.text)
items = contents.split(sep='\n')
for item in items.copy():
if item == '' or '(see ' in item:
items.remove(item)
#not sure why list includes empty strings
elif ',' in item:
text = item.split(sep=', ')
items.remove(item)
items.append(text[1] + ' ' + text[0])
#fixes 'lastname, firstname' convention
return items
def write_items(item_list):
"""Writes the contents of a list to a csv file
list of str -> none"""
filename = input("Please enter a name for the file to be created: ")
with open(filename, 'w', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
for item in item_list:
writer.writerow([item])
#without brackets around item, the writer object splits each word
#into individual characters separated by commas