-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtianyanchaInterceptHRX.py
252 lines (217 loc) · 7.83 KB
/
tianyanchaInterceptHRX.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
##!/usr/bin/python
# -*- coding: UTF-8 -*-
import random
import time
import traceback
import MySQLdb
from selenium import webdriver
from Config import phantomPath, USER_AGENTS, DAVIDPASSWD
from geetest import autoSubmitCha
from proxy.ipproxy import getAvailableIPs
# mysqlPool = None
conn = None
csor = None
def getConnCsor():
global conn,csor
if conn and csor:
return conn,csor
from DBUtils.PooledDB import PooledDB
# global mysqlPool
# if not mysqlPool:
mysqlPool = PooledDB(creator=MySQLdb, mincached=1, maxcached=2,
host="10.10.1.29", port=3306, user="root",
# host=NODE1IP, port=44408, user="crawler",
# host="10.24.161.94", port=44408, user="crawler",
passwd=DAVIDPASSWD, db="com", use_unicode=True, charset='utf8')
# passwd=NODEPASSWD, db="com_info", use_unicode=True, charset='utf8')
conn2 = mysqlPool.connection()
csor2 = conn2.cursor()
# conn.set_character_set('utf8')
csor2.execute('SET NAMES utf8')
csor2.execute("SET CHARACTER SET utf8")
csor2.execute("SET character_set_connection=utf8")
return conn2,csor2
def insertOrgContent(qid, content):
conn, csor = getConnCsor()
csor.execute('insert ignore tyc_org_content (id, org_content) values(%s, %s)',(qid,content))
conn.commit()
def loadWithUrl(browser,url = None):
# document.getElementById("interceptedResponse").innerHTML +=
# '{"data":' + self.responseText + '}*****';
time.sleep(random.random())
newUrl = browser.current_url
if 'antirobot.tianyancha.com/captcha' in newUrl:
autoSubmitCha(browser)
if 'antirobot.tianyancha.com/captcha' in newUrl:
return None
if not url:
url = browser.current_url
qid = url.replace('http://www.tianyancha.com/company/','')
errorPage = browser.find_elements_by_css_selector('.errorpage')
if errorPage and len(errorPage) > 0:
print 'content not found, qid:',qid
return
if '403' in browser.title or 'Forbidden' in browser.title:
print 'forbidden, reflash ip'
return None
if not len(browser.page_source) < 200:
insertOrgContent(qid, browser.page_source)
return 'ok'
# print browser.execute_script('return companyDetailJson;')
def getNewBrowser(url):
service_args, caps = getNewBrowserArgs()
driver = webdriver.PhantomJS(executable_path=phantomPath, service_args=service_args, desired_capabilities=caps)
# driver = webdriver.PhantomJS(executable_path=phantomPath, desired_capabilities=caps)
driver.set_page_load_timeout(100)
# driver.set_window_size(1366,768)
# driver.execute_script("var companyDetailJson = ''")
# browser.execute_script("var near = '';")
# browser.execute_script("var near = '';")
driver.get(url)
# driver.execute_script("""
# (function(XHR) {
# "use strict";
#
# var element = document.createElement('div');
# element.id = "interceptedResponse";
# element.appendChild(document.createTextNode(""));
# document.body.appendChild(element);
#
# var open = XHR.prototype.open;
# var send = XHR.prototype.send;
#
# XHR.prototype.open = function(method, url, async, user, pass) {
# this._url = url; // want to track the url requested
# open.call(this, method, url, async, user, pass);
# };
#
# XHR.prototype.send = function(data) {
# var self = this;
# var oldOnReadyStateChange;
# var url = this._url;
#
# function onReadyStateChange() {
# if(self.status === 200 && self.readyState == 4 /* complete */) {
# if(window.companyDetailJson){
# window.companyDetailJson = window.companyDetailJson + self.responseText;
# }else{
#
# }
# companyDetailJson = companyDetailJson + self.responseText;
# }
# if(oldOnReadyStateChange) {
# oldOnReadyStateChange();
# }
# }
#
# if(this.addEventListener) {
# this.addEventListener("readystatechange", onReadyStateChange,
# false);
# } else {
# oldOnReadyStateChange = this.onreadystatechange;
# this.onreadystatechange = onReadyStateChange;
# }
# send.call(this, data);
# }
# })(XMLHttpRequest);
# """)
# s = requests.Session()
# cooks = driver.get_cookies()
# cookies = dict()
# for co in cooks:
# cookies[co['name']] = co['value']
#
# print s.get(url + '.json',cookies= cookies).text
return driver
def getNewBrowserArgs():
pIPs = getAvailableIPs()
pipObj = random.choice(pIPs)
# randomPIpIndex = random.randint(0, len(pIPs))
# pipObj = pIPs[randomPIpIndex]
pIp = pipObj[0]
pPort = pipObj[1]
# ua = random.choice(USER_AGENTS)
# caps["phantomjs.page.settings.userAgent"] = ua
# proxy = webdriver.Proxy()
# proxy.proxy_type = ProxyType.MANUAL
# proxy.http_proxy = pIp + ':' + str(pPort)
# # 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
# proxy.add_to_capabilities(caps)
# driver.start_session(caps)
# if driver:
# try:
# # driver.close()
# driver.quit()
# except Exception as er:
# print er
caps = webdriver.DesiredCapabilities.PHANTOMJS
ua = random.choice(USER_AGENTS)
caps["phantomjs.page.settings.userAgent"] = ua
service_args = [
'--proxy=' + pIp + ':' + str(pPort),
'--proxy-type=http',
]
return service_args,caps
def start(f=2321660000, t=2321661823):
baseUrl = 'http://www.tianyancha.com/company/'
step = f
qidCarry = 1000
newBrowserCarry = 100
newBCount = 0
driver = None
while step <= t:
seq = range(step, step + qidCarry )
random.shuffle(seq)
for qid in seq:
try:
url = baseUrl + str(qid)
# url = baseUrl + str(2320040778)
if newBCount == 0 or newBCount >= newBrowserCarry:
# if newBCount != 0:
newBCount = 1
if driver: # 先关闭上一个
try:
driver.quit()
except Exception as se:
print 'se'
driver = getNewBrowser(url)
while not loadWithUrl(driver, url = url):
driver = getNewBrowser(url)
# try:
# driver.close() #只关闭标签页
# except Exception as ce:
# print 'ce'
else:
driver.get(url)
while not loadWithUrl(driver, url = url):
driver = getNewBrowser(url)
# try:
# driver.close() #只关闭标签页
# except Exception as ce:
# print 'ce'
newBCount = newBCount + 1
except Exception as e:
print 'fail and not retry. qid: ',str(qid), 'e:',traceback.format_exc()
newBCount == 0
if step == t:
print 'Done!'
break
step = step + qidCarry
step = step if step < t else t
driver = getNewBrowser('http://www.tianyancha.com/company/3793676')
loadWithUrl(driver)
def testChrome():
driver = webdriver.Chrome(executable_path='/Users/zyq/env/chromedriver')
# driver = webdriver.Chrome()
driver.get('http://www.qichacha.com/firm_c3ece65bad28c17cc7f67168448e50e1.shtml');
print driver.current_url
print driver.page_source
if __name__ == '__main__':
f = 25998711
t = 2321661823
import sys
if len(sys.argv) > 2:
f = int(sys.argv[1])
t = int(sys.argv[2])
# start(f,t)
testChrome()