python爬取个性签名的方法

yipeiwu_com6年前Python爬虫

本文实例为大家分享了python爬取个性签名的具体代码,具体内容如下

#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设计】。

相关文章

Python爬虫辅助利器PyQuery模块的安装使用攻略

Windows下的安装: 下载地址:https://pypi.python.org/pypi/pyquery/#downloads 下载后安装: C:\Python27>ea...

python制作爬虫并将抓取结果保存到excel中

python制作爬虫并将抓取结果保存到excel中

学习Python也有一段时间了,各种理论知识大体上也算略知一二了,今天就进入实战演练:通过Python来编写一个拉勾网薪资调查的小爬虫。 第一步:分析网站的请求过程 我们在查看拉勾网上的...

Python爬虫实例_利用百度地图API批量获取城市所有的POI点

Python爬虫实例_利用百度地图API批量获取城市所有的POI点

上篇关于爬虫的文章,我们讲解了如何运用Python的requests及BeautifuiSoup模块来完成静态网页的爬取,总结过程,网页爬虫本质就两步: 1、设置请求参数(url,hea...

一则python3的简单爬虫代码

不得不说python的上手非常简单。在网上找了一下,大都是python2的帖子,于是随手写了个python3的。代码非常简单就不解释了,直接贴代码。 复制代码 代码如下:#test rd...

python爬取51job电子书信息并入库的实现代码

入门级爬虫:只抓取书籍名称,信息及下载地址并存储到数据库数据库工具类:DBUtil.pyimport pymysql class DBUtils(object):...