-
Notifications
You must be signed in to change notification settings - Fork 0
/
finished_code.py
34 lines (29 loc) · 1.05 KB
/
finished_code.py
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
import requests
from bs4 import BeautifulSoup
# Part A: Define URL to scrape and the item you would like to search
URL = 'https://removeandreplace.com/2013/09/24/complete-list-can-recycle/'
ITEM = input("Item to recycle?: ")
# Part B: Grab the data with a GET request
r = requests.get(URL)
# Part C: Parse the HTML using Beautiful Soup
soup = BeautifulSoup(r.text, 'html.parser')
items = soup.findAll('strong')
# Part D: Filter out items that can be recycled and clean output
recyclable = []
include = False
for item in items:
val = item.text # Extract text from the HTML tag
if val == 'YOU CAN RECYCLE:': # Include items following this tag
include = True
elif val == 'YOU CANNOT RECYCLE:': # Stop including items when you encounter this tag
include = False
else:
if include:
recyclable.append(val)
# Part E: Print out the relevant searched items
print('----------------')
for item in recyclable:
if ITEM in item:
print('> ' + item)
print('----------------')
print('-End of Results-')