-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper-v2.py
More file actions
73 lines (48 loc) · 2.02 KB
/
Copy pathscraper-v2.py
File metadata and controls
73 lines (48 loc) · 2.02 KB
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
# As of 6/23/2017, this script parses OJS issues (NOT ARTICLES) exported to an XML file, places the tags in a "header" list and the contents in a contents list.
# It breaks at section and article tags and starts a new line.
# Last edited on 8/22/2017 for Himalayan Research Papers
# importing libraries
import xml.etree.cElementTree as ET
# creating XML tree
tree = ET.parse("issues (10).xml")
root = tree.getroot()
data_headers = []
data_raw = []
data_contents = []
## open file to write data
text_file = open("issue_data.txt", "a")
# getting tags and text and putting them into lists
for node in tree.iter():
#skipping nodes that have no content, or useless content
if node.tag == 'embed' or node.tag == 'open_access' or node.tag == 'image' or node.tag == 'galley' or node.tag == 'label' or node.tag == 'file':
continue
#excluding section and article tags -- these have children but no content
if node.tag != 'section' and node.tag != 'article':
data_headers.append(node.tag)
data_raw.append(node.text)
#print data_raw
else:
#converting null values to string
for i in data_raw:
if i is None:
data_contents.append('None')
else:
data_contents.append(i)
#print data_headers
#print data_contents
#converting lists to strings for writing to file
header_str = '|'.join(data_headers).encode('utf-8').strip()
content_str = '|'.join(data_contents).encode('utf-8').strip()
#print header_str, content_str
## for TEXT FILE - write header row, tag row, then clear, then write next header/tag row
text_file.write(header_str)
text_file.write('\n')
text_file.write(content_str)
text_file.write('\n')
## empty lists for next pass
del data_headers[:]
del data_raw[:]
del data_contents[:]
## do not need to clear strings because string will be reassigned from list which is repopulated anew for each item
## close file
text_file.close()