python爬取個性簽名的方法
更新時間:2018年06月17日 13:16:54 作者:隱名_C
這篇文章主要為大家詳細介紹了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。這種情況下,要進行編碼轉(zhuǎn)換,都需要先用
decode方法將其轉(zhuǎn)換成unicode編碼,再使用encode方法將其轉(zhuǎn)換成其他編碼。通常,在沒有指定特定的編碼方式時,都是使用的系統(tǒng)默認編碼創(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)文章
詳解pandas中利用DataFrame對象的.loc[]、.iloc[]方法抽取數(shù)據(jù)
這篇文章主要介紹了pandas中利用DataFrame對象的.loc[]、.iloc[]方法抽取數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python 中的 dataclass使用場景與代碼示例詳解
在Python中,dataclass是一個裝飾器,用于簡化類的定義,自動生成初始化、比較等方法,適用于需要存儲數(shù)據(jù)的場景,通過示例展示了dataclass的基本用法,感興趣的朋友跟隨小編一起看看吧2024-09-09
Python創(chuàng)建普通菜單示例【基于win32ui模塊】
這篇文章主要介紹了Python創(chuàng)建普通菜單,結(jié)合實例形式分析了Python基于win32ui模塊創(chuàng)建普通菜單及添加菜單項的相關(guān)操作技巧,并附帶說明了win32ui模塊的安裝命令,需要的朋友可以參考下2018-05-05

