使用python怎么爬取個性簽名?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。
具體內容如下
#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內部的表示是unicode編碼,因此,在做編碼轉換時,通常需要以unicode作為中間編碼,
即先將其他編碼的字符串解碼(decode)成unicode,再從unicode編碼(encode)成另一種編碼。
decode的作用是將其他編碼的字符串轉換成unicode編碼,如str1.decode('gb2312'),表示將gb2312編碼的字符串str1轉換成unicode編碼。
encode的作用是將unicode編碼轉換成其他編碼的字符串,如str2.encode('gb2312'),表示將unicode編碼的字符串str2轉換成gb2312編碼。
總得意思:想要將其他的編碼轉換成utf-8必須先將其解碼成unicode然后重新編碼成utf-8,它是以unicode為轉換媒介的
如:s='中文'
如果是在utf8的文件中,該字符串就是utf8編碼,如果是在gb2312的文件中,則其編碼為gb2312。這種情況下,要進行編碼轉換,都需要先用
decode方法將其轉換成unicode編碼,再使用encode方法將其轉換成其他編碼。通常,在沒有指定特定的編碼方式時,都是使用的系統默認編碼創建的代碼文件。
如下:
s.decode('utf-8').encode('utf-8')
decode():是解碼
encode()是編碼
isinstance(s,unicode):判斷s是否是unicode編碼,如果是就返回true,否則返回false*
'''
if not name:
messagebox.showinfo('提示','請輸入姓名再設計!')
return
data = {
'word':name,
'sizes':'60',
#'fonts':'jfcs.ttf', # 個性簽名
#'fonts':'qmt.ttf', # 連筆簽名
'fonts': 'bzcs.ttf',# 瀟灑簽名
#'fonts':'lfc.ttf',# 草體簽名
#'fonts':'haku.ttf',# 和文簽名
#'fonts':'zql.ttf',# 商務簽名
#'fonts':'yak.ttf',# 可愛簽名
'fontcolor':'#000000'
}
result = requests.post(start_url,data = data).content
reg = '<div class="tu">.*<img src="(.*?)"/></div>'# 截止20180302 網站CSS變動
result = bytes.decode(result) # byte轉換成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('個性簽名設計')
root.geometry('+800+300')# 設置窗口出現在屏幕上面的位置
Label(root,text='姓名',font = ('微軟雅黑',15)).grid() # 布局方法不要混用
entry = Entry(root,font=('微軟雅黑',15))
entry.grid(row=0,column=1)
button = Button(root,text='設計簽名',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 = '設計簽名',font = ('微軟雅黑',20),command = download).grid(row = 1,column = 0)
root.mainloop()
'''關于使用python怎么爬取個性簽名問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。