forked from bluedazzle/python_network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetSpider
112 lines (97 loc) · 3.53 KB
/
NetSpider
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
import urllib.request
import optparse
import http.cookiejar
import urllib.parse
import time
import re
import os
import copy
class NetSpider(object):
"""docstring for NetSpider"""
def __init__(self):
self.__mainurl = ""
self.__host = ""
self.__refer = ""
self.__accept = "text/html, application/xhtml+xml, */*"
self.__contenttype = "application/x-www-form-urlencoded"
self.__useragent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"
self.__cookielist = http.cookiejar.CookieJar()
self.__opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.__cookielist))
urllib.request.install_opener(self.__opener)
self.__postHeaders = {'Host':self.__host,
'Accept':self.__accept,
'Refer':self.__refer,
'Content-Type':self.__contenttype,
'User-Agent':self.__useragent}
"""属性"""
def getMainUrl(self): return self.__mainurl
def setMainUrl(self, value):self.__mainurl = value
MainUrl = property(getMainUrl, setMainUrl, "Property MainUrl")
def getHost(self): return self.__host
def setHost(self, value):
self.__host = value
self.__postHeaders['Host'] = value
Host = property(getHost, setHost, "Property Host")
def getRefer(self): return self.__refer
def setRefer(self, value):
self.__refer = value
self.__postHeaders['Refer'] = value
Refer = property(getRefer, setRefer, "Property Refer")
def getAccept(self): return self.__accept
def setAccept(self, value):
self.__accept = value
self.__postHeaders['Accept'] = value
Accept = property(getAccept, setAccept, "Property Accept")
def getContentType(self): return self.__contenttype
def setContentType(self, value):
self.__contenttype = value
self.__postHeaders['Content-Type'] = value
ContentType = property(getContentType, setContentType, "Property ContentType")
def getUserAgent(self): return self.__useragent
def setUserAgent(self, value):
self.__useragent = value
self.__postHeaders['User-Agent'] = value
UserAgent = property(getUserAgent, setUserAgent, "Property UserAgent")
def getCookieList(self): return self.__cookielist
def setCookieList(self, value):self.__cookielist = value
CookieList = property(getCookieList, setCookieList, "Property CookieList")
def getPostHeaders(self): return self.__postHeaders
def setPostHeaders(self, value): self.__postHeaders = value
PostHeaders = property(getPostHeaders, setPostHeaders, "Property PostHeaders")
"""私有方法"""
def __ErrorHandle(self,errtype):
print('发生错误',end = ':')
print(errtype)
"""方法"""
def GetResFromRequest(self,method,requrl,encodemethod = 'gbk',postdict = {''},reqdata = ''):
try:
if method == 'POST':
if reqdata != '':
req = urllib.request.Request(requrl, reqdata)
else:
postData = urllib.parse.urlencode(postDict).encode()
req = urllib.request.Request(requrl, postData)
elif method == "GET":
req = urllib.request.Request(requrl)
for key,itm in self.__postHeaders.items():
req.add_header(key,itm)
res = urllib.request.urlopen(req)
return res.read().decode(encodemethod)
except Exception as e:
self.__ErrorHandle(e)
else:
pass
finally:
pass
def SearchCookie(self,searchkey):
for index, cookie in enumerate(self.__cookielist):
if(cookie.name == searchkey):
return cookie.value
return 'nothing find'
def ShowCurrentCookie(self):
if len(self.__cookielist) == 0:
print("未获取到cookie")
return 0
for index, cookie in enumerate(self.__cookielist):
print(index,end=':')
print(str(cookie.name) + ' : ' + str(cookie.value))