|
| 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 | + |
0 commit comments