python爬取個性簽名的方法
更新時間:2018年06月17日 13:16:54 作者:隱名_C
這篇文章主要為大家詳細(xì)介紹了python爬取個性簽名的方法,具有一定的參考價值,感興趣的朋友可以參考一下
本文實例為大家分享了python爬取個性簽名的具體代碼,具體內(nèi)容如下
#coding:utf-8
#import tkinter
from tkinter import *
from tkinter import messagebox
import requests
import re
from PIL import Image
def download():
start_url = 'http://www.uustv.com/'
name = entry.get().encode('utf-8')
'''
*首先要搞清楚,字符串在Python內(nèi)部的表示是unicode編碼,因此,在做編碼轉(zhuǎn)換時,通常需要以unicode作為中間編碼,
即先將其他編碼的字符串解碼(decode)成unicode,再從unicode編碼(encode)成另一種編碼。
decode的作用是將其他編碼的字符串轉(zhuǎn)換成unicode編碼,如str1.decode('gb2312'),表示將gb2312編碼的字符串str1轉(zhuǎn)換成unicode編碼。
encode的作用是將unicode編碼轉(zhuǎn)換成其他編碼的字符串,如str2.encode('gb2312'),表示將unicode編碼的字符串str2轉(zhuǎn)換成gb2312編碼。
總得意思:想要將其他的編碼轉(zhuǎn)換成utf-8必須先將其解碼成unicode然后重新編碼成utf-8,它是以unicode為轉(zhuǎn)換媒介的
如:s='中文'
如果是在utf8的文件中,該字符串就是utf8編碼,如果是在gb2312的文件中,則其編碼為gb2312。這種情況下,要進(jìn)行編碼轉(zhuǎn)換,都需要先用
decode方法將其轉(zhuǎn)換成unicode編碼,再使用encode方法將其轉(zhuǎn)換成其他編碼。通常,在沒有指定特定的編碼方式時,都是使用的系統(tǒng)默認(rèn)編碼創(chuàng)建的代碼文件。
如下:
s.decode('utf-8').encode('utf-8')
decode():是解碼
encode()是編碼
isinstance(s,unicode):判斷s是否是unicode編碼,如果是就返回true,否則返回false*
'''
if not name:
messagebox.showinfo('提示','請輸入姓名再設(shè)計!')
return
data = {
'word':name,
'sizes':'60',
#'fonts':'jfcs.ttf', # 個性簽名
#'fonts':'qmt.ttf', # 連筆簽名
'fonts': 'bzcs.ttf',# 瀟灑簽名
#'fonts':'lfc.ttf',# 草體簽名
#'fonts':'haku.ttf',# 和文簽名
#'fonts':'zql.ttf',# 商務(wù)簽名
#'fonts':'yak.ttf',# 可愛簽名
'fontcolor':'#000000'
}
result = requests.post(start_url,data = data).content
reg = '<div class="tu">.*<img src="(.*?)"/></div>'# 截止20180302 網(wǎng)站CSS變動
result = bytes.decode(result) # byte轉(zhuǎn)換成string
img_url = start_url + re.findall(reg,result)[0]
name = 'tmp' # 避免了源代碼在win下無法正常寫入文件的問題
response = requests.get(img_url).content
# 將生成的簽名圖片下載到本地
with open('{}.gif'.format(name),'wb')as f:
f.write(response)
try:
im = Image.open('{}.gif'.format(name))
im.show()
except:
print("自己打開看吧!")
root = Tk()
root.title('個性簽名設(shè)計')
root.geometry('+800+300')# 設(shè)置窗口出現(xiàn)在屏幕上面的位置
Label(root,text='姓名',font = ('微軟雅黑',15)).grid() # 布局方法不要混用
entry = Entry(root,font=('微軟雅黑',15))
entry.grid(row=0,column=1)
button = Button(root,text='設(shè)計簽名',font=('微軟雅黑',15),width = '10',height = 1,command = download)
button.grid(row=1,column=1)
root.mainloop()
'''
from tkinter import *
import requests
from tkinter import messagebox
import re
from PIL import Image,ImageTk
def download():
startUrl = 'http://www.uustv.com/'
name = entry.get()
if not name:
messagebox.showinfo('提示','請輸入名字!')
else:
data = {
'word':name,
'sizes':'60',
'fonts':'jfcs.ttf',
'fontcolor':'#000000'
}
result = requests.post(startUrl,data = data)
result.encoding = 'utf-8'
req = '<div class="tu"><img src="(.*?)"/></div>'
imgUrl = startUrl+(re.findall(req,result.text)[0])
response = requests.get(imgUrl).content
with open('{}.gif'.format(name),'wb') as f:
f.write(response)
#im = Image.open('{}.gif'.format(name))
#im.show()
bm = ImageTk.PhotoImage(file = 'E:\py\{}.gif'.format(name))
label2 = Label(root, image = bm)
label2.bm = bm
label2.grid(row = 2,columnspan = 2)
root = Tk()
root.title('GUI')
root.geometry('600x300')
root.geometry('+500+200')
label = Label(root,text = '簽名',font = ('華文行楷',20))
label.grid(row=0,column = 0)
entry = Entry(root,font = ('微軟雅黑',20))
entry.grid(row = 0,column = 1)
Button(root,text = '設(shè)計簽名',font = ('微軟雅黑',20),command = download).grid(row = 1,column = 0)
root.mainloop()
'''
以上全部為本篇文章的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)RabbitMQ6種消息模型的示例代碼
這篇文章主要介紹了Python實現(xiàn)RabbitMQ6種消息模型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python實現(xiàn)批量讀取HDF多波段柵格數(shù)據(jù)并繪制像元直方圖
這篇文章主要為大家詳細(xì)介紹了如何基于Python語言gdal模塊,實現(xiàn)多波段HDF柵格圖像文件的讀取、處理與像元值可視化(直方圖繪制)等操作,需要的可以參考一下2023-03-03
VSCODE配置Markdown及Markdown基礎(chǔ)語法詳解
這篇文章主要介紹了VSCODE配置Markdown及Markdown基礎(chǔ)語法詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
詳解Python OpenCV圖像分割算法的實現(xiàn)
圖像分割是指根據(jù)灰度、色彩、空間紋理、幾何形狀等特征把圖像劃分成若干個互不相交的區(qū)域。本文就來和大家聊聊OpenCV的圖像分割算法及基于輪廓的字符分離,感興趣的可以了解一下2022-08-08
解決pyecharts在jupyter notebook中使用報錯問題
這篇文章主要介紹了解決pyecharts在jupyter notebook中使用報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06

