isExists = os.path.exists(down_path) if not isExists: os.makedirs(down_path) print(down_path + ' 创建成功') else: # 如果目录存在则不创建,并提示目录已存在 print(down_path + ' 目录已存在') 查看文件是否存在
if not isExists:
os.makedirs(down_path)
print(down_path + ' 创建成功')
else:
# 如果目录存在则不创建,并提示目录已存在
print(down_path + ' 目录已存在')
查看文件是否存在
import requestsimport json
import urllib
import os.path
headers = {'Accept':'application/json'}
response = requests.get('https://manhua.163.com/book/catalog/5015165829890111936.json',headers=headers)
response = json.loads(response.text)
print(type(response))
index=1
print(response['catalog']['sections'])
for i in response['catalog']['sections']:
for j in i['sections']:
# print(j['title'])
titleName=str(index)+' '+j['title']
print(titleName)
index+=1
isExists = os.path.exists(titleName)
# 判断结果
if not isExists:
os.makedirs(titleName)
print(titleName + ' 创建成功')
else:
# 如果目录存在则不创建,并提示目录已存在
print(titleName + ' 目录已存在')
爬虫设置
print(''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)))产生随机的五个字符
Token产生与验证
import timeimport base64
import hmac
def generate_token(key, expire=7260):
ts_str = str(time.time() + expire)
ts_byte = ts_str.encode("utf-8")
sha1_tshex_str = hmac.new(key.encode("utf-8"), ts_byte, 'sha1').hexdigest()
token = ts_str+':'+sha1_tshex_str
b64_token = base64.urlsafe_b64encode(token.encode("utf-8"))
return b64_token.decode("utf-8")
def certify_token(key, token):
token_str = base64.urlsafe_b64decode(token).decode('utf-8')
token_list = token_str.split(':')
if len(token_list) != 2:
return False
ts_str = token_list[0]
if float(ts_str) < time.time():
return False
known_sha1_tsstr = token_list[1]
sha1 = hmac.new(key.encode("utf-8"), ts_str.encode('utf-8'), 'sha1')
calc_sha1_tsstr = sha1.hexdigest()
if calc_sha1_tsstr != known_sha1_tsstr:
return False
return True
mykey = "fdjaljfal"
mykey="mytoken64"
mytoken = generate_token(mykey)
print(mytoken)
mytoken="MTU1ODM3NzAzOS41NjQwNTgzOmFkZmIxYzYzY2EyMGFmMzRiNDgwMmY1ZTQ1NWFkMmQyOTdhNzcwN2E="
print(certify_token(mykey,mytoken))
图形验证码
import base64import random
from io import BytesIO
values = ''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5))
print(values)
from captcha.image import ImageCaptcha
img = ImageCaptcha(width=160, height=60, fonts=['./wqy.ttf'], font_sizes=None)
image = img.generate_image(values)
image.show()
output_buffer = BytesIO()
image.save(output_buffer, format='JPEG')
byte_data = output_buffer.getvalue()
base64_str = base64.b64encode(byte_data).decode("utf-8")
pic="data:image/jpeg;base64,"+str(base64_str)
print(pic)
print(base64_str)
文件上传
def post(self):file = self.request.files.get('file')
r = []
for f in file:
hash = hashlib.sha256()
hash.update(f['body'])
filename = hash.hexdigest()
_, ext = os.path.splitext(f['filename'])
if ext:
filename += ext
with open(f'{STATIC_PATH}/{filename}', 'wb') as i:
i.write(f['body'])
r.append(f'/static/{filename}')
self.json(r)