forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMianText.py
More file actions
43 lines (27 loc) · 840 Bytes
/
MianText.py
File metadata and controls
43 lines (27 loc) · 840 Bytes
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
#!/usr/bin.env python
from HTMLParser import HTMLParser
from re import sub
import urllib2
import sys
class HtmlParserMainText(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.text = []
def handle_starttag(self, tag, attrs):
if tag == "p":
self.text.append("\n")
def handle_data(self, data):
if len(data.strip()) > 0:
self.text.append(data.strip())
def GetMainText():
url = "http://www.bbc.com/"
html = urllib2.urlopen(url).read()
html_code = sub('<script[^>]*?>[^>]*?</script>','',html) #delete all scripts
parser = HtmlParserMainText()
parser.feed(html_code)
parser.close()
return ''.join(parser.text).strip()
if __name__ == "__main__":
reload(sys)
sys.setdefaultencoding('utf-8')
print GetMainText()