forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0008.py
More file actions
23 lines (22 loc) · 747 Bytes
/
Copy path0008.py
File metadata and controls
23 lines (22 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#coding=utf-8
#参考http://blog.csdn.net/gzlaiyonghao/article/details/1741185和http://www.cnblogs.com/jasondan/p/3497757.html的算法思路
#但是第二篇的政治观点并不认同
from __future__ import division
import re,urllib2
import HTMLParser
from lxml import etree
html_parser = HTMLParser.HTMLParser()
res = urllib2.urlopen("""http://www.cnblogs.com/jasondan/p/3497757.html""").read()
D = etree.HTML(res.decode('utf-8', 'ignore'))
F = D.xpath(u"//script")
for i in F:
i.text = ""
res = etree.tostring(D)
res = re.sub("<[^>]>",">\n",res)
res = re.split("[\r\n]+",res)
for line in res:
if not line:
continue
con = re.sub(("<[^>]*>"),"",line)
if len(con)/len(line)>0.5:
print (html_parser.unescape(con)).encode("GBK","ignore")