-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.py
515 lines (462 loc) · 18.4 KB
/
browser.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import re
import urllib
import urllib.request as req
import gzip
from io import BytesIO
import json
import time
from pprint import pprint
from urllib import parse
import socket
import sys,os
from lxml import html
import random
def printc(strcolor,*argc,**kwargs):
if strcolor == "red":
colortype = '31'
elif strcolor == 'green':
colortype = '32'
elif strcolor == 'yellow':
colortype = '33'
elif strcolor == 'blue':
colortype ='34'
elif strcolor == 'pink':
colortype = '35'
elif strcolor == 'cyan':
colortype = '36'
elif strcolor == 'white':
colortype = '37'
print('\033[{}m'.format(colortype),end='',flush=True)
print(*argc,**kwargs,flush=True)
print('\033[0m',end='',flush=True)
class Base:
def __init__(self):
raise NotImplementedError('action must be defined')
def save(self,fname):
raise NotImplementedError('action must be defined')
def load(self,fname):
raise NotImplementedError('action must be defined')
class Browser(Base):
def __init__(self,savef='',cookiestack=None,timeout=5,retry=2,unicode='utf-8'):
self.webpages = {}
self.cookiestack = cookiestack or Cookiestack()
self.timeout = timeout
self.retry = retry
self.unicode = unicode
self.stacks = []
self.cookiestack.delexpires()
self.User_agent = self.get_user_agent()
def snapshot(self,fname,cookiefname):
webpages_l = [{'url':web.url,
'html' if web.html else 'read':web.html or str(web.read),
'headers':web.headers,
'User_Agent':web.User_Agent,
'data':web.data,
'method':web.method,
'param':web.param} for web in self.webpages.values()]
d = {'webpages':webpages_l,
'cookiestackfname':cookiefname,
'timeout':self.timeout,
'retry':self.retry,
'unicode':self.unicode,
'User_agent':self.User_agent}
with open(fname,'w') as f:
jstxt = json.dumps(d)
f.write(jstxt)
self.cookiesave(cookiefname)
def cookiesave(self,fname):
self.cookiestack.save(fname)
def load(self,fname):
with open(fname,'r') as f:
d = json.loads(f.read())
print(d)
self.timeout = d['timeout']
self.unicode = d['unicode']
self.User_agent = d['User_agent']
self.cookiestack = Cookiestack()
self.cookiestack.loadall(d['cookiestackfname'])
self.cookiestack.delexpires()
self.webpages = []
for web in d['webpages']:
webobj = Webpage(web['url'],cookiestack=self.cookiestack,\
timeout=self.timeout,retry=self.retry,\
codec=self.codec,data=web['data'],method=web['method'],\
param=web['param'])
if 'html' in web:
webobj.html = web['html']
else:
webobj.read = eval(web['read'])
self.webpages.append(webobj)
def browse(self,url,fname=None,save=False,nocookie=False,unicode=None,\
timeout=None,retry=None,data=None,param=None,method='GET',\
nohtml=False):
cookiestack = self.cookiestack if not nocookie else None
web = Webpage(url,User_agent=self.User_agent,cookiestack=cookiestack, \
timeout=timeout or self.timeout,retry=retry or self.retry,\
unicode=unicode or self.unicode,data=data,param=param,method=method)
web.get(nohtml=nohtml)
if not self.cookiestack.update(web):
return web
if fname or save:
fname = fname or web.url+'.save'
mode = 'w' if web.html else 'wb'
with open(fname,mode) as f:
try:
f.write(web.html or web.read)
except TypeError as e:
web.reget(nohtml=nohtml)
self.webpages[web.url] = web
return web
def get_user_agent(self):
UserAgent = ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0',
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
'Mozilla/5.0(Macintosh;IntelMacOSX10.6;rv:2.0.1)Gecko/20100101Firefox/4.0.1',
'Opera/9.80(Macintosh;IntelMacOSX10.6.8;U;en)Presto/2.8.131Version/11.11'
]
max = len(UserAgent) - 1
using_agent = UserAgent[random.randint(0, max)]
print('User-agent: ', using_agent)
return using_agent
def addcookies(self,webdom,cookiedict):
self.cookiestack.addcookie(webdom,cookiedict)
def selebrowse(self,func):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
browser = webdriver.Firefox()
func(browser)
for ele in browser.get_cookies():
self.cookiestack.adddictcookie(ele["domain"],ele)
def __call__(self, *args, **kwargs):
return self.browse(*args,**kwargs)
class Cookiestack(Base):
def __init__(self,fname='cookie.1'):
self.cookies = {}
self.savefile = fname
self.load()
self.delexpires()
self.filep = open(fname,'a')
for webdom,dct in self.cookies.items():
for v in dct.values():
self.save(webdom,v)
# {'name':name,
# 'value':value,
# keys:vals}
# including:
# 'expires' , 'secure' , 'path' etc.
def addcookie(self,webdom,string_list,debug=False):
if webdom not in self.cookies.keys():
self.cookies[webdom] = {}
d = {}
for ele in string_list:
l = ele.split(';')
print('cookie list:',l)
i, max = 0, len(l)
while i < max:
str = l[i]
if str == 'Secure':
d['Secure'] = 'True'
elif str == 'HttpOnly':
d['HttpOnly'] = 'True'
elif '=' in str:
# print(ele)
[key, val] = str.split('=')
if i == 0:
d['name'] = key
d['value'] = val
else:
d[key] = val
i += 1
if debug:
if d['name'] in self.cookies[webdom]:
print('Update:', d['name'], d['value'])
else:
print('Add:', d['name'], d['value'])
self.cookies[webdom][d['name']] = d
self.save(webdom,d)
return 1
def update(self,web,debug=False):
webdom = UrlList(web.url)[2]
if webdom not in self.cookies:
self.cookies[webdom] = {}
if not web.response:
return 0
for ele in web.response.headers._headers:
if ele[0] == 'Set-Cookie':
self.addcookie(webdom,[ele[1]],debug=debug)
return 1
def getcookies(self,url,isdom=False):
if url == '':
printc('red','Error: url should not be empty')
webdom = UrlList(url)[2]
res = {}
if webdom not in self.cookies:
printc('red','Cookies of domain',webdom,'cannot be found.')
return res
for (name,dval) in self.cookies[webdom].items():
res[name] = dval['value']
return res
def getstrcookie(self,url,isdom=False):
d = self.getcookies(url,isdom)
return ';'.join(['{}={}'.format(name,key) for (name,key) in d.items()])
def delexpires(self):
for domd in self.cookies:
for ele in domd:
if 'expires' not in ele:
continue
exptime = time.strptime(ele['expires'],'%a, %d-%b-%Y %H:%M:%S GMT')
if time.gmtime() > exptime:
domd.pop(ele)
def save(self,webdom,d):
print("saving:",webdom,d)
dct = {'webdom':webdom,
'd':d}
self.filep.write(json.dumps(dct)+'\n')
def load(self):
if not self.savefile in os.listdir():
return 0
with open(self.savefile,'r') as fp:
fp.seek(0,0)
for l in fp.readlines():
if l == '\n':
continue
print(l)
dct = json.loads(l)
print('loads:',dct)
webdom = dct['webdom']
d = dct ['d']
if webdom not in self.cookies.keys():
self.cookies[webdom] = {}
self.cookies[webdom][d['name']] = d
def domchk(self,url):
print('checking url:',url)
webdom = UrlList(url)
return webdom[2] in self.cookies.keys()
def show(self):
for (doms,d) in self.cookies.items():
print('-'*50)
print(doms)
print('-'*15)
for dict in d.values():
for (k,v) in dict.items():
print(k,v)
print()
print('-'*50)
def adddictcookie(self,webdom,dict):
if webdom not in self.cookies.keys():
self.cookies[webdom] = {}
self.cookies[webdom][dict['name']] = dict
def __add__(self, other):
return self.cookies.update(other.cookies)
def __str__(self):
return 'Cookie contains {} site(s) \n {} '.format(self.cookies.__len__(), \
'\n'.join(self.cookies.keys()))
def __getitem__(self, item):
return self.cookies[item]
def __call__(self, *args, **kwargs):
self.updates(*args,**kwargs)
def __exit__(self):
self.filep.close()
class Webpage(Base):
def __init__(self,url,cookiestack=None,timeout=10,retry=2, \
User_agent=None,unicode='utf8',data:dict=None,method='GET',param:dict=None,\
header:dict=None):
self.url = url
self.headers = header or {}
self.param:dict = param or {}
self.request = None
self.response = None
self.method = method
self.html = None
self.read = None
self.unicode = unicode
self.data:dict = data or {}
self.cookiestack = cookiestack
self.User_Agent = User_agent
self.timeout = timeout
self.retry = retry
def get(self,*argv,timeout=2, retry=2, debug=False, \
nohtml=False,unverifiable=False):
if not self.url:
return None
if self.data:
data = parse.urlencode(self.data).encode('ascii')
else:
data = None
if not self.url.startswith('https://') and not self.url.startswith('http://'):
self.url = 'https://' + self.url
if 'User-Agent' not in self.headers and self.User_Agent:
self.headers['User-Agent'] = self.User_Agent
self.headers.update(argv)
self.addcookies()
print('Start downloading:', self.url)
try:
self.request = req.Request(self.url, headers=self.headers, data=data)
self.request.param = self.param
self.method = 'GET' if not self.data else 'POST'
self.request.method = self.method
if debug:
print(self.request.data)
self.response = req.urlopen(self.request, timeout=timeout)
except (urllib.error.URLError,socket.timeout) as e:
print('Download error No.', e.errno,end='')
if hasattr(e,'reason'):
print('\treason:',e.reason)
else:
print()
if hasattr(e, 'code') and 500 <= e.code < 600:
print('Error code:',e.code)
self.reget(timeout=timeout, retry=retry - 1, debug=debug,nohtml=nohtml)
try:
_read = BytesIO()
bs , reading , total = 1024 * 8 , 0 , 0
if 'Content-Length' in self.response.headers:
content_length = int(self.response.getheader('Content-Length'))
else:
content_length = 0
print('\033[32m', end='')
while True:
block = self.response.read(bs)
if not block:
break
reading += len(block)
if content_length:
print('\rLoading:{:4.2%}\t{} bytes.'.format(reading/content_length,reading) \
,end='')
else:
print('\rLoading:{} bytes.'.format(reading),end='')
_read.write(block)
print()
_read.flush()
self.read = _read.getvalue()
print('\033[0m',end='')
if len(self.read) == 0 or (content_length != 0 and reading < content_length):
printc('red','Error: retrival incomplite. {} / {} bytes'.format(reading,content_length))
self.reget(timeout=timeout, retry=retry - 1, debug=debug,nohtml=nohtml)
except socket.timeout as e:
printc('red','Socket timeout:{}'.format(e))
time.sleep(1)
self.reget(timeout=timeout, retry=retry - 1, debug=debug,nohtml=nohtml)
except:
return self.url
self.encode = self.response.getheader('Content-Encoding')
if debug:
print(self.read, '\nEncode:', self.encode)
# decode if html is a gzip file
if self.encode == 'gzip':
byte = BytesIO(self.read)
file = gzip.GzipFile(fileobj=byte)
self.read = file.read()
if nohtml:
#print('Return webpage.read')
return self.read
try:
self.html = self.read.decode(self.unicode, errors='ignore')
except (TypeError,AttributeError) as e:
printc('red','Occured Error:{}',format(e))
time.sleep(2)
self.reget(timeout=timeout,retry = retry -1,debug=debug,nohtml=nohtml)
print('Return webpage.html')
return self.html
def reget(self,timeout=2, retry=2, debug=False, \
nohtml=False,sleep=1):
printc('yellow','sleep',sleep,'seconds')
time.sleep(sleep)
if retry == 0:
printc('yellow','Retry remains 0, sleep 5 s.')
time.sleep(5)
ans = input('Retry is now zero, do you still want' + \
' to retry after failing this time?')
retry = 1 if ans == 'Yes' or ans == 'Y' \
or ans == 'yes' or ans == 'y' or ans == '' else retry
if retry > 0:
return self.get(timeout=timeout, retry=retry - 1, debug=debug,nohtml=nohtml)
else:
return None
def xpath(self,path):
if not self.html:
return []
tree = html.fromstring(self.html.encode(self.unicode))
return tree.xpath(path)
def addcookies(self, cookiestack=None):
cookiestack = cookiestack or self.cookiestack
if not cookiestack:
return -1
if cookiestack.domchk(self.url):
self.headers['Cookie'] = cookiestack.getstrcookie(self.url)
def save(self,fname=''):
fname = fname or (self.url+'.save' if self.html else self.url+'.byte')
mode = 'w' if self.html else 'wb'
with open(fname,mode) as f:
print('save mode:',mode)
f.write(self.html or self.read)
class UrlList():
def __init__(self,url='',scheme='',internet='',dom='',port='',*path,ftype='.html'):
self.urllist = [scheme,internet,dom,port,*path,ftype] \
if not url else self.urlsplit(url)
self.fullurl = url or ''
def getfullurl(self,update=False):
if self.fullurl and not update:
return self.fullurl
self.fullurl = self.urllist[0] + '://' + self.urllist[1] + '.' + self.urllist[2] + \
('' if not self.urllist[3] else (':'+self.urllist[3])) + \
'/' + '/'.join(self.urllist[5:-1]) + '.' + self.urllist[-1]
return self.fullurl
def urlsplit(self, url):
if '://' in url:
[scheme, specificpart] = url.split('://')
else:
scheme, specificpart = '', url
if '?' in specificpart:
mainpart,data = specificpart.split('?')
params = data.split('&')
else:
mainpart = specificpart
params = []
split = mainpart.split('/')
resl = re.findall('([^\.]*\.)?([^:\.]*\.[^:\.]*)(:[0-9]*)?', split[0])
if resl:
(internet, domain, port) = resl[0]
else:
(internet, domain, port) = ('', '', '')
if len(split) > 1:
if '.' in split[-1]:
type = split[-1].split('.')[-1]
f = '.'.join(split[-1].split('.')[:-1])
else:
f, type = split[-1], ''
self.urllist = [scheme, internet[:-1], domain, port[1:],*split[1:-1],f,*params,type]
else:
self.urllist = [scheme, internet[:-1], domain, port[1:],*params]
#print(self.urllist)
return self.urllist
def __getitem__(self, item):
return self.urllist[item]
def __str__(self):
return str(self.urllist)
if __name__ == "__main__":
browser = Browser(timeout=10)
forum = browser.browse('https://bbs.yamibo.com',codec='gbk')
sendmail_src = forum.xpath('//script')[-3].attrib['src']
send_web = browser(forum.url + '/' + sendmail_src,codec='gbk')
form = browser('https://bbs.yamibo.com/member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login',\
codec = 'gbk')
formhash = form.xpath('//input[@type="hidden"]')[0].attrib['value']
action = form.xpath('//form')[0].attrib['action']
data = {'formhash':formhash,
'referer':'https://bbs.yamibo.com/forum.php',
'loginfield':'username',
'username':'',
'password':'',
'questionid':'0',
'answer':''}
post = browser.browse('https://bbs.yamibo.com/' + action,data=data,codec='gbk')
forum = browser.browse('https://bbs.yamibo.com', codec='gbk')
browser.cookiestack.show()
browser.save('browser.1')
del browser
b2 = Browser(fname='browser.1')
print(b2.webpages)