保存文本文档函数
'''
file_path:创建文件的保存的位置
message:创建文件的内容
'''
def creat_file(file_path, message):
f = open(file_path, mode='a', encoding=('utf-8'))
f.write(message)
f.write('\n')
f.close()
'''
例如:
message= '创建文本文件'
creat_file(r"C:\Users\Administrator\Desktop\message.txt",message)
'''
截取想要的中间字符串
'''
从一个字符串中截取中间需要的部分
content:字符串
startStr:开始位置
endStr:结束位置
'''
def GetMiddleStr(content, startStr, endStr):
startIndex = content.index(startStr)
if startIndex >= 0:
startIndex += len(startStr)
endIndex = content.index(endStr)
return content[startIndex:endIndex]
'''
例如:
html = '从一个字符串中截取中间需要的部分'
text = GetMiddleStr(html,'一个','中')
print(text) # 字符串
'''
链接下载图片
'''
url:图片下载链接地址
file_path:图片保存路径
file_name:图片名字
'''
import os,requests
def GetPicture(url,file_path,file_name):
File_Name = file_path + file_name + ".jpg"
try:
if not os.path.exists(file_path):
os.mkdir(file_path)
if not os.path.exists(File_Name):
th = requests.get(url)
with open(File_Name, "wb") as f:
f.write(th.content)
f.close()
print("文件保存成功")
else:
print("文件已经存在")
except:
print("下载失败")
'''
例如:
url2 = ['https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201803%2F16%2F20180316213958_myqyt.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634889031&t=6ef64358a98e2abbfda8a86145fecd1f']
root = r"C:\Users\Administrator\Desktop\ "
title2 = ['阿狸','阿猫']
for url3 in url2:
for title in title2:
GetPicture(url3,root,title)
'''
?把ISO-8859-1编码转化为UTF-8
def ZM(string):
ISO = string.encode('ISO-8859-1') #先编码
return ISO.decode('utf-8')# 再解码
|