Skip to content

Commit 232ed7f

Browse files
committed
added 0008 and 0009
1 parent 5c986e1 commit 232ed7f

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎robot527/0008/find_text.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! usr/bin/python3
2+
"""
3+
第 0008 题:一个HTML文件,找出里面的正文。
4+
"""
5+
6+
import re, urllib.request, time
7+
url = input('Enter the URL which you wish to extract > ')
8+
if '' == url:
9+
url = "https://adblockplus.org/zh_CN/acceptable-ads"
10+
11+
print('We will extract text data from ' + url + ' :')
12+
13+
14+
with urllib.request.urlopen(url) as response:
15+
content = response.read()
16+
try:
17+
content = content.decode('utf-8')
18+
except UnicodeDecodeError:
19+
content = content.decode('gbk')
20+
#remove some special content
21+
content = re.sub(r'<title.+?</title>', '', content, flags = re.DOTALL)
22+
content = re.sub(r'<head(er)?.+?</head(er)?>', '',
23+
content, flags = re.DOTALL)
24+
content = re.sub(r'<(no)?script.+?</(no)?script>', '',
25+
content, flags = re.DOTALL)
26+
content = re.sub(r'<style.+?</style>', '', content, flags = re.DOTALL)
27+
content = re.sub(r'<form.+?</form>', '', content, flags = re.DOTALL)
28+
content = re.sub(r'<footer.+?</footer>', '', content, flags = re.DOTALL)
29+
#find text and fill them in a list
30+
result = re.findall(r'(?<=>)[^><]+?(?=<)', content)
31+
text = ''.join(result).strip()
32+
file_name = time.strftime('%Y%m%d%H%M%S') + '.txt'
33+
with open(file_name, 'wt') as textfile:
34+
#textfile.write(text)
35+
print(text, file = textfile)
36+
print('Extract finished, the text file is: ' + file_name)
37+
38+

‎robot527/0009/find_links.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#! usr/bin/python3
2+
"""
3+
第 0009 题:一个HTML文件,找出里面的链接。
4+
"""
5+
6+
import re, urllib.request
7+
url = input('Enter the URL which you wish to extract > ')
8+
if '' == url:
9+
url = "https://adblockplus.org/zh_CN/acceptable-ads"
10+
11+
print('We will extract links from ' + url + ' :')
12+
13+
14+
with urllib.request.urlopen(url) as response:
15+
content = response.read()
16+
try:
17+
content = content.decode('utf-8')
18+
except UnicodeDecodeError:
19+
content = content.decode('gbk')
20+
link_list = re.findall('''(?<=href=)["'](.[^"']+)["']''', content)
21+
i = 1
22+
for item in link_list:
23+
print('%d:' % i, item)
24+
i += 1

0 commit comments

Comments
 (0)