forked from sheepzh/poetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshige_cc_web.py
71 lines (55 loc) · 1.79 KB
/
shige_cc_web.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
import re
import requests
from bs4 import BeautifulSoup
from util import write_poem, Profile, remove_tmp_all
domain = 'http://shige.cc'
poet_reg = re.compile(r'^・(.*?)・$')
module_reg = re.compile(r'^【.*?】$')
title_reg = re.compile(r'◇(.*?)◇')
term_hrefs = []
def parse_poem(text, year):
lines = text.split('\n')
poet_name = ''
title = ''
content = []
for line in lines:
line = line.strip().replace('\ufffd', '')
if '~ ~ ※ ~ ~' in line or module_reg.match(line):
if title:
write_poem(Profile(title=title, author=poet_name, href=''),
'\r\n'.join(content)+'\r\n'+year)
title = ''
continue
poet = poet_reg.findall(line)
if len(poet):
poet_name = poet[0]
continue
title_maybe = title_reg.findall(line)
if len(title_maybe):
print(title_maybe, title, len(content))
if title:
write_poem(Profile(title=title, author=poet_name, href=''),
'\r\n'.join(content)+'\r\n'+year)
title = title_maybe[0]
content = []
else:
content.append(line)
def read_col(td):
year_reg = re.compile(r'^(\d{4}年\d{1,2}月).*')
lis = td.find_all('li')
for li in lis:
a = li.find('a')
if not a:
continue
href = a.get('href')
year = year_reg.findall(a.text)[0]
response = requests.get(domain+href)
response.encoding = 'gb2312'
parse_poem(response.text, year)
def main():
directory = requests.get('http://shige.cc/web/dir.php')
soup = BeautifulSoup(directory.text, 'lxml')
tables = soup.find_all('table')
read_col(tables[1])
remove_tmp_all()
main()