-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetchlistfromhtml.py
75 lines (54 loc) · 1.46 KB
/
getchlistfromhtml.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
72
73
74
75
# -*- coding: utf-8 -*-
import urllib2
from bs4 import BeautifulSoup
import re
import time
import jieba
import sys
import readCSV
def getChListFromHtml(url):
#url="https://www.zhihu.com/question/49019162#answer-41274457"
try:
user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
header = {"User-Agent":user_agent}
req = urllib2.Request(url,headers=header)
resp = urllib2.urlopen(req)
html = resp.read()
#html=urllib2.urlopen(url).read()
html=unicode(html,'utf-8')
word=re.findall(ur"[\u4e00-\u9fa5]+",html)
s=""
for w in word:
s+=w
seg_list=jieba.cut(s,cut_all=False)
wdlist = list(seg_list)
#print "read "+url[0:25]+" -------- OK ! "
#fenci=",".join(wdlist)
#print 'get web-->',s
#print 'div result-》',fenci
return wdlist
#time.sleep(10)
except:
emptylist = []
return emptylist
if __name__=="__main__":
url = "http://blog.csdn.net/nevasun/article/details/7331644"
#urlArr = readCSV.getUrl("browserHistory.csv")
wdlist = getChListFromHtml(url)
fenci=",".join(wdlist)
#restore the string:fenci
if(url.find('csdn')!=-1):
filename = "CSDN.txt"
elif(url.find('zhihu')!=-1):
filename = "zhihu.txt"
elif(url.find('bupt')!=-1):
filename = "buptbbs.txt"
elif(url.find('ustc')!=-1):
filename = "ustcbbs.txt"
elif(url.find('baidu')!=-1):
filename = "baiduQA.txt"
else:
filename = "othersites.txt"
fo = open(filename,'a')
fo.write(fenci.encode('utf-8'))
fo.close()