-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetdata.py
More file actions
executable file
·216 lines (191 loc) · 5.63 KB
/
getdata.py
File metadata and controls
executable file
·216 lines (191 loc) · 5.63 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
import re
import os
import urllib2
import string
import urllib2
import os
import sys
import chardet
from HTMLParser import HTMLParser
from readChinese import *
import threading
def getHtml(url):
from urllib2 import HTTPError
try:
html=""
page = urllib2.urlopen(url,timeout = 10)
html = page.read();
typeEncode = sys.getfilesystemencoding()
infoencode = chardet.detect(html).get('encoding','utf-8')
infoconfidence = chardet.detect(html).get('confidence')
#print type(infoconfidence)
print chardet.detect(html)
if infoconfidence != 0.99 : #预防乱码 只取0.99识别率的
return ""
html = html.decode(infoencode,'ignore').encode(typeEncode)
except Exception as err:
print "getHtml err"
print(err)
finally:
return html
def getUrlFromHtml( str_html ):
listUrl = []
str_html=str_html.replace(" ","")
urls=re.findall(r"<a.*?href=.*?<\/a>",str_html,re.I) #<a href="http://money.163.com/">财经</a></h2>
for url in urls:
#print ("getUrlFromHtml -> %s " %(url) )
removeTagList=['jpg','apk','download','img','gif','js']
for tag in removeTagList:
if tag in url:
pass
pos_start=url.find("http:")
if pos_start == -1:
pass
else:
pos_end = url.find("\"", pos_start)
if pos_end == -1:
pass
else:
url = url[pos_start:pos_end]
#print url
if len(url)<=0:
pass
'''
#only get tieba data
if "http://tieba.baidu.com" in url: #只抓取贴吧内容。
print("tieba: %s" %(url) )
listUrl.append( url)
#print 'this is over'
'''
else:
#print("url: %s" %(url) )
listUrl.append( url)
return listUrl
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.data = []
def get_data(self):
return self.data
def handle_data(self, data):
#print "====="+data
#print "-----"+self.lasttag
list_tag = ['div','li','ul','a','img','i','b','p','font','span','td','th','title']
#list_tag = ['br','p','a']
if self.lasttag in list_tag:
if data != None and len(data.strip()) > 0:
#print "-----"+self.lasttag
#print "====="+data
s = data.strip()
self.data.append(s)
def getTextFromHtml( str_html ):
try:
htmlParser = MyHTMLParser()
htmlParser.feed(str_html)
htmlParser.close()
return htmlParser.get_data()
except Exception as err:
print "getTextFromHtml"
print(err)
return ""
'''
for line in htmlParser.get_data():
print line
'''
def getDocSize(path):
try:
size = os.path.getsize(path)
return size
except Exception as err:
print "getDocSize error"
print(err)
def writeTextToFile( strFilePath, list_text ):
fo = open(strFilePath, "a+")
lines = fo.readlines();
lines_set = set([line.strip() for line in lines])
new_line_set = set(list_text)
add_line_set = new_line_set - lines_set
list_text = list(add_line_set)
for i in xrange( len( list_text )):
fo.write( list_text[i] )
fo.write( "\n" )
fo.close()
urlAddressPool = set()
def determineAddToUrl( store_url, list_url ):
global urlAddressPool
for url_temp in list_url:
if url_temp in urlAddressPool:
pass
else:
urlAddressPool.add(url_temp)
store_url.append(url_temp)
#print store_url
store_url = [];
store_text = [];
fileSize = 5*1024*1024*1024
filePath = "text.txt"
def firstStartReptiles():
global store_url
global store_text
global fileSize
global filePath
html = getHtml("http://www.163.com/");
#html = getHtml("https://tieba.baidu.com/p/5145295063");
list_url = getUrlFromHtml(html)
list_text = getTextFromHtml(html)
store_text.extend( list_text );
determineAddToUrl( store_url, list_url )
writeTextToFile(filePath,store_text)
#print store_url
#print store_text
mylock = threading.RLock()
myThreadGetHtmlTextlock = threading.RLock()
taskNumber = 100
myThreadtaskNumberlock = threading.RLock()
class myThreadGetHtmlText( threading.Thread ):
def __init__(self,url):
threading.Thread.__init__(self)
self.url = url
def run(self):
global store_text
global store_url
global taskNumber
global filePath
html=getHtml(self.url)
print("1 get url -> %s html over" %(self.url) )
if False == DetermineToPickHtml( html ,"chinese.txt"):
print "throw html ,determine false"
pass
list_url = getUrlFromHtml(html)
print("2 get list_url over")
myThreadGetHtmlTextlock.acquire()
determineAddToUrl( store_url, list_url )
myThreadGetHtmlTextlock.release()
print("3 add to store_url over")
list_text = getTextFromHtml(html)
print("4 get list_text over")
myThreadGetHtmlTextlock.acquire()
writeTextToFile(filePath,list_text)
myThreadGetHtmlTextlock.release()
print("5 add to store_text over")
if __name__ == '__main__':
firstStartReptiles()
while True:
tsk = []
for task in xrange(taskNumber):
myThreadGetHtmlTextlock.acquire()
url = store_url.pop(0);
myThreadGetHtmlTextlock.release()
print("0 pop url -> %s over" %(url) )
threadGetHtml = myThreadGetHtmlText(url)
threadGetHtml.start()
tsk.append(threadGetHtml)
for tt in tsk:
tt.join(10)
#getDocSize(filePath ) > fileSize:
print ("=========================================")
print ("store_url size: %d" %( len( store_url ) ) )
print ("urlAddressPool size: %d"%(len(urlAddressPool)))
print "loop"
print ("=========================================")