-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_down.py
138 lines (115 loc) · 3.66 KB
/
thread_down.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
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib2
import re
import sys
from threading import Thread
import time
import random
import hashlib
import Queue
from bs4 import BeautifulSoup
import os
import MySQLdb
import logging
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s] [%(thread)d] %(filename)s[line:%(lineno)d] [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
#filename='thread_down.log',
filemode='w')
class MysqlThread(Thread):
sql_queue=Queue.Queue(0)
conn =None
cur=None
def __init__(self):
self.conn=MySQLdb.connect(host='localhost',user='root',db='x365x_data',charset='utf8')
self.cur=self.conn.cursor()
Thread.__init__(self)
self.start()
def __del__(self ):
self.cur.close()
self.conn.close()
Thread.__del__(self)
logging.info( "MysqlThread exit!")
def AddSql(self, sql):
logging.info( "add task:[%s] " % sql)
self.sql_queue.put(sql)
def run(self):
while True:
try:
sql=self.sql_queue.get()
if sql == "EXIT!":
logging.info( "MySQLThread BREAK!")
break
logging.info( "sql exe:[%s]" % sql)
self.cur.execute(sql)
self.conn.commit()
except MySQLdb.Error,e:
logging.info( "Mysql Error %d: %s" % (e.args[0], e.args[1]))
break
db_proxy=MysqlThread()
def DownloadPage(curUrl):
htmlPage=""
try:
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req = urllib2.Request(url=curUrl, headers = headers)
res = urllib2.urlopen(req)
headers = res.info()
htmlPage = res.read()
#logging.info( headers['Content-Encoding'])
if ('Content-Encoding' in headers and headers['Content-Encoding']) or \
('content-encoding' in headers and headers['content-encoding']):
logging.info( "Gzip-ing")
import gzip
import StringIO
data = StringIO.StringIO(htmlPage)
gz = gzip.GzipFile(fileobj=data)
htmlPage = gz.read()
gz.close()
except Exception,e:
logging.info( "download page [%s] err: %s." % (curUrl, str(e)))
return htmlPage
def CrawlThreadImage(tid):
url_queue=Queue.Queue(0)
threadUrl="http://meizi.us/thread-%d-1-1.html" % tid
url_queue.put(threadUrl)
seqID=int(0)
while url_queue.qsize() > 0:
url=url_queue.get()
logging.info( "parse photo page:[%s]", url)
page=DownloadPage(url).decode('utf8','ignore')
rc='<img src="*([^" ]*.jpg)"*[^>]*'
images = re.findall(rc, page, re.MULTILINE | re.DOTALL)
for image in images:
seqID+=1
image=filter(lambda ch: ch not in ' \'"', image)
logging.info( "\tcrawled img on[%d]: [%s]\n" % (tid,image))
#img_queue.put((image,localfile))
db_proxy.AddSql("insert ignore into thread_imgs(tid,idx,url,state)values(%d,%d,'%s',0);" % (tid,seqID,image))
soup=BeautifulSoup(page)
nextTag=soup.findAll('a',text=re.compile(ur'^[\u4e00-\u9fa5]+\>$'))
if len(nextTag) > 0:
url_queue.put(nextTag[0]['href'])
updateSql="update crawled_thread set state= %d , picnum=%d where tid=%d;" % (1, seqID, tid)
db_proxy.AddSql(updateSql)
def startThreadDown():
#db_proxy=MysqlThread()
sql="select tid,dir from crawled_thread where state = 0 and views > 3 order by pubtime desc;"
conn=MySQLdb.connect(host='localhost',user='root',db='x365x_data',charset='utf8')
cur=conn.cursor()
count=cur.execute(sql)
logging.info( "total get %d threads from DB\n" % count)
results = cur.fetchall()
cur.close()
conn.close()
for res in results:
CrawlThreadImage(res[0])
db_proxy.AddSql("EXIT!")
if db_proxy.isAlive(): db_proxy.join()
logging.info("ThreadDown end")
return count
def start():
while startThreadDown():
time.sleep(1)
if __name__ == '__main__':
start()