from http import cookiejar
from urllib import request
class Craw():
def __init__(self):
self.url = ''
self.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
def getCookies(self):
cookie = cookiejar.CookieJar()
handler = request.HTTPCookieProcessor(cookie)
opener = request.build_opener(handler)
response = opener.open(self.url)
cookieValue = ''
for item in cookie:
cookieValue += item.name + '=' + item.value + ';'
self.headers['Cookie'] = cookieValue
response = requests.get(url=self.url)
def getVerificationCode(self):
img_url = ''
imgResponse = requests.get(url=img_url,headers = self.headers) #直接使用headers即可
base64_jpg = base64.b64encode(imgResponse.content)
return base64_jpg