-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
179 lines (164 loc) · 6.61 KB
/
crawler.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
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
# -*- coding: utf-8 -*-
import binascii
import sys
from urlparse import urlparse
reload(sys)
sys.setdefaultencoding('utf8')
__author__ = 'patmue'
import beanstalkc
import pycurl
from BeautifulSoup import BeautifulSoup
import MySQLdb
from StringIO import StringIO
import json
# Connection
beanstalk = beanstalkc.Connection(host='localhost', port=11300)
beanstalk.watch('url_list')
def info(curl):
"Return a dictionary with all info on the last response."
m = {}
m['effective_url'] = curl.getinfo(pycurl.EFFECTIVE_URL)
m['http_code'] = curl.getinfo(pycurl.HTTP_CODE)
m['total_time'] = curl.getinfo(pycurl.TOTAL_TIME)
m['namelookup_time'] = curl.getinfo(pycurl.NAMELOOKUP_TIME)
m['connect_time'] = curl.getinfo(pycurl.CONNECT_TIME)
m['pretransfer_time'] = curl.getinfo(pycurl.PRETRANSFER_TIME)
m['redirect_time'] = curl.getinfo(pycurl.REDIRECT_TIME)
m['redirect_count'] = curl.getinfo(pycurl.REDIRECT_COUNT)
m['size_upload'] = curl.getinfo(pycurl.SIZE_UPLOAD)
m['size_download'] = curl.getinfo(pycurl.SIZE_DOWNLOAD)
m['speed_upload'] = curl.getinfo(pycurl.SPEED_UPLOAD)
m['header_size'] = curl.getinfo(pycurl.HEADER_SIZE)
m['request_size'] = curl.getinfo(pycurl.REQUEST_SIZE)
m['content_length_download'] = curl.getinfo(pycurl.CONTENT_LENGTH_DOWNLOAD)
m['content_length_upload'] = curl.getinfo(pycurl.CONTENT_LENGTH_UPLOAD)
m['content_type'] = curl.getinfo(pycurl.CONTENT_TYPE)
m['response_code'] = curl.getinfo(pycurl.RESPONSE_CODE)
m['speed_download'] = curl.getinfo(pycurl.SPEED_DOWNLOAD)
m['ssl_verifyresult'] = curl.getinfo(pycurl.SSL_VERIFYRESULT)
m['filetime'] = curl.getinfo(pycurl.INFO_FILETIME)
m['starttransfer_time'] = curl.getinfo(pycurl.STARTTRANSFER_TIME)
m['redirect_time'] = curl.getinfo(pycurl.REDIRECT_TIME)
m['redirect_count'] = curl.getinfo(pycurl.REDIRECT_COUNT)
m['http_connectcode'] = curl.getinfo(pycurl.HTTP_CONNECTCODE)
m['httpauth_avail'] = curl.getinfo(pycurl.HTTPAUTH_AVAIL)
m['proxyauth_avail'] = curl.getinfo(pycurl.PROXYAUTH_AVAIL)
m['os_errno'] = curl.getinfo(pycurl.OS_ERRNO)
m['num-connects'] = curl.getinfo(pycurl.NUM_CONNECTS)
m['ssl_engines'] = curl.getinfo(pycurl.SSL_ENGINES)
m['cookielist'] = curl.getinfo(pycurl.INFO_COOKIELIST)
m['lastsocket'] = curl.getinfo(pycurl.LASTSOCKET)
m['ftp_entry_path'] = curl.getinfo(pycurl.FTP_ENTRY_PATH)
return m
while True:
job = beanstalk.reserve(timeout=0)
# print (job.body)
#print(job.delete())
skip = 0
if job:
# print (job.body)
content = json.loads(job.body)
url = content["domain"].encode("utf8")
parent_domain = content["parent_domain"].encode("utf8")
if(-1 != job.body.find("#")):
continue
print "catch job" + url
db = MySQLdb.connect(host="localhost", user="root", passwd="xxxxx", init_command='SET NAMES utf8mb4',
use_unicode=True)
cursor = db.cursor()
cursor.execute(""" SELECT id FROM spider.urllist WHERE crc_url = %s AND url = %s
LIMIT 1
""",(
str(binascii.crc32(url))
,url
))
for (id) in cursor:
if id > 0:
skip = 1
print "skip " + url
if skip == 0:
buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.CONNECTTIMEOUT, 500)
c.setopt(c.AUTOREFERER, 1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.MAXREDIRS, 10)
CURLOPT_ENCODING = getattr(pycurl, 'ACCEPT_ENCODING', pycurl.ENCODING)
c.setopt(CURLOPT_ENCODING, "utf-8")
c.setopt(c.TIMEOUT, 500)
c.setopt(c.USERAGENT, 'Crawler: Patmue v1.0 (http://www.x303.de) ')
c.perform()
curinfo = info(c)
# HTTP response code, e.g. 200.
c.close()
text = buffer.getvalue()
soup = BeautifulSoup(text)
text = text.decode(soup.originalEncoding)
cursor.execute("""INSERT INTO spider.urllist (
`url`
, `crc_url`
, `content`
, `domain`
, `http_code`
, `total_time`
, `namelookup_time`
, `connect_time`
, `pretransfer_time`
, `redirect_time`
, `redirect_count`
, `size_download`
, `header_size`
, `content_length_download`
, `content_type`
, `response_code`
, `speed_download`
, `filetime`
, `http_connectcode`
)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
( url
, binascii.crc32(url)
, text.decode('Latin-1').decode('utf8')
, str(urlparse(url).hostname)
, curinfo['http_code']
, curinfo['total_time']
, curinfo['namelookup_time']
, curinfo['connect_time']
, curinfo['pretransfer_time']
, curinfo['redirect_time']
, curinfo['redirect_count']
, curinfo['size_download']
, curinfo['header_size']
, curinfo['content_length_download']
, curinfo['content_type']
, curinfo['response_code']
, curinfo['speed_download']
, curinfo['filetime']
, curinfo['http_connectcode']
)
)
db.commit()
#parent_domain
cursor.execute("""INSERT INTO spider.link_list (
`crc_root_domain`
, `root_domain`
, `crc_parent_domain`
, `parent_domain`
)
VALUES (%s,%s,%s,%s)""",
( binascii.crc32(url)
, url
, binascii.crc32(parent_domain)
, parent_domain
)
)
db.commit()
beanstalkSecond = beanstalkc.Connection(host='localhost', port=11300)
beanstalkSecond.use("analyser_url")
beanstalkSecond.put(str(cursor.lastrowid))
skip = 0
job.delete()
print "no job"