-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stage2.py
55 lines (40 loc) · 1.47 KB
/
Stage2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from bs4 import BeautifulSoup
import os
import requests
import csv
import time
with open('Stage1.csv', mode='r') as infile:
reader = csv.reader(infile)
mydict = {rows[0]:rows[1] for rows in reader}
#
secondphaselinksandfilenames = {}
for url,filename in mydict.items():
if os.path.isfile(filename) is False:
page = (requests.get(url))
soup = BeautifulSoup(page.text, "html.parser")
with open(filename, "w") as myfile:
myfile.write(str(soup))
with open(filename,'r') as f:
contents = f.read()
soup = BeautifulSoup(contents, "html.parser")
for unwanted in soup("input"):
unwanted.decompose()
soup = soup.select('.navlevel1, .navlevel2')
if os.path.isfile(filename) is False:
os.remove(filename)
for items in soup:
with open(filename, "a") as myfile: myfile.write(str(items))
for links in soup:
test = (links.find('a').attrs)
filename = (os.path.basename(test.get('href')))
path = ((os.path.dirname(test.get('href'))))
fullurl = 'http://edwards.yale.edu/archive' + path + '/' + filename
fullfilename = filename + '.html'
secondphaselinksandfilenames[fullurl] = fullfilename
for key,val in secondphaselinksandfilenames.items():
print (key)
print (val)
import csv
with open('stage2.csv', 'w') as f:
for key in secondphaselinksandfilenames.keys():
f.write("%s,%s\n" % (key, secondphaselinksandfilenames[key]))