-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.py
103 lines (71 loc) · 2.93 KB
/
pull.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
from Bio.Entrez import efetch
import xml.etree.ElementTree as ET
import pandas
from BeautifulSoup import BeautifulSoup
import csv
import pandas as pd
def fetch_abstract():
colnames = ['pmids']
data = pandas.read_csv("pmids.csv", names=colnames)
pmids = list(data.pmids)
full = []
for i in pmids:
handle = efetch(db='pubmed', id=i, retmode='xml')
xml_data = handle.read()
soup = BeautifulSoup(xml_data)
a_recs = []
for tag in soup.findAll("pubmedarticle"):
title = tag.articletitle.text
journal = tag.findAll("journal")
try:
for info in journal:
year = info.find("year").text
except:
for info in journal:
year = info.find("year")
for a_tag in tag.findAll("author"):
a_rec = {}
a_rec['year'] = year
a_rec['title'] = title
a_rec['pmid'] = int(tag.pmid.text)
try:
a_rec['lastname'] = str(a_tag.lastname.text)
except:
a_rec['lastname'] = str(a_tag.lastname)
try:
a_rec['forename'] = str(a_tag.forename.text)
except:
a_rec['forename'] = str(a_tag.forename)
try:
a_rec['initials'] = str(a_tag.initials.text)
except:
a_rec['initials'] = str(a_tag.initials)
try:
a_rec['affiliation'] = str(a_tag.affiliation.text)
except:
a_rec['affiliation'] = str(a_tag.affiliation)
a_recs.append(a_rec)
#full.append(a_recs)
full.append(a_recs)
def convert(input):
if isinstance(input, dict):
return {convert(key): convert(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [convert(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
full = convert(full)
print "running"
#article = ET.XML(xml_data)
#print article.find('AuthorList').findall('Author')
for entry in full:
#print entry
keys = ['year', 'title', 'pmid', 'lastname', 'forename', 'initials', 'affiliation']
f = open("citations.csv", "ab")
dict_writer = csv.DictWriter(f, keys)
dict_writer.writer.writerow(keys)
dict_writer.writerows(entry)
return
fetch_abstract()