-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_combine.py
More file actions
48 lines (44 loc) · 1.69 KB
/
Copy pathpost_combine.py
File metadata and controls
48 lines (44 loc) · 1.69 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
from cgitb import text
import requests
from bs4 import BeautifulSoup
from time import sleep
import re
from xml.dom import minidom
import os
import random
def load_links(link_filename):
with open('post_content' + os.sep + link_filename, 'r') as f:
lines = f.readlines()
lines = [line.rstrip() for line in lines]
return lines
def data2xml(permaID, post_content, post_reaction, post_comment):
xmlfile = minidom.Document()
node = xmlfile.createElement('d')
node.setAttribute('n' , 'post_' + permaID)
node.setAttribute('book' , permaID)
node_text = xmlfile.createTextNode(post_content)
node.appendChild(node_text)
xmlfile.appendChild(node)
return xmlfile
if __name__ == '__main__':
illegal_xml_re = re.compile(u'[\x00-\x08\x0b-\x1f\x7f-\x84\x86-\x9f\ud800-\udfff\ufdd0-\ufddf\ufffe-\uffff]')
with open('allpost.xml', 'w') as f:
f.write('<?xml version="1.0" encoding="UTF-8"?><b n="articles_modified" book=""> <b n="articles_modified">\n')
with open('allpost.txt', 'w') as f:
pass
for filename in os.listdir('post_content'):
with open('allpost.xml', 'a', encoding='utf-8') as f:
f.write('<l>')
contents = load_links(filename)
for content in contents:
content = illegal_xml_re.sub('', str(content).encode('utf-8','ignore').decode("utf-8"))
with open('allpost.xml', 'a', encoding='utf-8') as f:
f.write(content + ' ')
with open('allpost.txt', 'a', encoding='utf-8') as f:
f.write(content + ' ')
with open('allpost.xml', 'a', encoding='utf-8') as f:
f.write('</l>\n')
with open('allpost.txt', 'a', encoding='utf-8') as f:
f.write('\n')
with open('allpost.xml', 'a', encoding='utf-8') as f:
f.write('</b></b>')