-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_scrape.py
111 lines (77 loc) · 2.57 KB
/
combine_scrape.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import numpy as np
import pandas as pd
links = []
x = 0
while(True):
temp = len(links)
URL = "https://exhibits.stanford.edu/oss-maps/browse/all-exhibit-items?page={}&per_page=96".format(x+1)
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
# print(soup.prettify())
for link in soup.find_all('a'):
pos = link.get('href')
if(pos.find("catalog")!=-1):
# links.append("https://exhibits.stanford.edu"+pos)
links.append("https://purl.stanford.edu"+pos[-12:])
if(len(links)==temp): break
else: x += 1
for link in links: print(link)
print(len(links))
browser = webdriver.Chrome()
seen = []
fin_dic = []
keywords = ["description", "contributors", "subjects", "bibliography-info"]
# counter = 0;
for URL in links:
if(URL in seen):
continue
print("Scraping: {}".format(URL))
browser.get(URL)
time.sleep(5)
try:
dic = {}
title = browser.find_element_by_xpath("/html/body/div[1]/div/div/section/h1").text
dic['TITLE'] = title
dic['URL'] = URL
for keyword in keywords:
elem = browser.find_element_by_id(keyword)
# lst = elem.find_element_by_tag_name("dl")
curr = ""
for item in elem.find_elements_by_xpath("div[@class='section-body']/dl/*"):
# print(item.tag_name)
if item.tag_name=="dt":
curr = item.text
dic[curr] = ""
elif curr!="" and item.tag_name=="dd":
dic[curr] += item.text+";"
if(curr==""):
for item in elem.find_elements_by_xpath("div[@class='section-body']/*"):
# print(item.tag_name)
if item.tag_name=="dt":
curr = item.text
dic[curr] = ""
elif curr!="" and item.tag_name=="dd":
dic[curr] += item.text+";"
browser.switch_to.frame(browser.find_element_by_tag_name("iframe"))
time.sleep(1)
browser.find_element_by_xpath("/html/body/div/div/div/div/main/div[1]/div[1]/section/div[1]/header/nav/div/button[3]").click()
time.sleep(1)
browser.find_element_by_xpath("/html/body/div/div/div/div/main/div[3]/div[3]/ul/li[2]").click()
time.sleep(1)
browser.find_elements_by_xpath("/html/body/div/div/div/div/main/div[3]/div[3]/div/div[2]/ul/li")[-1].find_element_by_xpath("div/span/a").click()
time.sleep(5)
fin_dic.append(dic)
# if(counter==10): break
# else: counter += 1
except:
print("Badness")
continue
seen.append(URL)
df = pd.DataFrame(fin_dic)
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
df.to_csv("./output.csv",index=False)