-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_orcids.py
52 lines (44 loc) · 1.7 KB
/
scrape_orcids.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
#simple web scraper to retrieve ORCIDs from Wolbach's ORCID webpage
import requests
from bs4 import BeautifulSoup
import csv
import cStringIO
import codecs
#UnicodeWriter stolen from Katie - makes csv writing run more smoothly
class UnicodeWriter:
def __init__(self, f, dialect=csv.excel, encoding="utf-8-sig", **kwds):
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
data = self.queue.getvalue()
data = data.decode("utf-8")
data = self.encoder.encode(data)
self.stream.write(data)
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
#creates a new csv to write to
fileout = codecs.open('wolbach_orcids_uni.csv','wb')
wr = UnicodeWriter(fileout,lineterminator='\n', delimiter='|', dialect='excel',quoting=csv.QUOTE_ALL)
#retrieves web content from Wolbach's ORCID page
url = "http://library.cfa.harvard.edu/orcidid"
page = requests.get(url)
html = page.content
#soupifies it and finds the table in the page
soup = BeautifulSoup(html)
orcid_table = soup.find('table')
#creates an array, and then drills down within the table to find each cell, which it appends to the array
orcrows = []
for row in orcid_table.findAll('tr'):
orccells = []
for cell in row.findAll('td'):
text = cell.text
orccells.append(text)
orcrows.append(orccells)
#writes the array to the csv file
wr.writerows(orcrows)
fileout.close()