当前位置: 移动技术网 > IT编程>脚本编程>Python > python简单爬虫 指定汉字的笔画动图下载

python简单爬虫 指定汉字的笔画动图下载

2020年07月03日  | 移动技术网IT编程  | 我要评论

python简单爬虫

指定汉字的笔画动图下载

分析过程

  1. 打开网址首页
    汉字屋网站首页

  2. 选择一个字,比如“虐”
    虐字

  3. 查看该汉字动图地址

    右键该汉字动图,选择在新标签页打开在这里插入图片描述
    得到该图片的地址
    https://www.hanzi5.com/assets/bishun/animation/8650-bishun.gif
    在这里插入图片描述
    这里也可以查看网页代码来得到汉字动图地址
    右键汉字动图,选择检查
    在这里插入图片描述
    在这里插入图片描述
    右侧控制台,显示
    在这里插入图片描述
    同样得到汉字动图地址

  4. 多查看几个动图的地址
    “掠”https://www.hanzi5.com/assets/bishun/animation/63a0-bishun.gif
    “扁”https://www.hanzi5.com/assets/bishun/animation/6241-bishun.gif
    “龙”https://www.hanzi5.com/assets/bishun/animation/9f99-bishun.gif
    猜测不同的部分应该是汉字对应的编码

  5. 确定动图地址不同的部分是什么编码类型(utf-8,unicode,gbk16进制字符)
    在控制台js环境输入 ‘\u9f99’ 得到 ,说明是Unicode编码
    在这里插入图片描述

  6. 接下来对汉字进行编码,得到所需编码
    在这里插入图片描述
    9f99提取出来
    在这里插入图片描述
    再将字节型转换为字符型
    在这里插入图片描述

  7. 将得到的所需编码填入到动图地址中更换,即可得到任意汉字的动图地址

  8. 根据动图地址将图片下载即可

代码实现

import requests

def get_gif(word):
    url="https://www.hanzi5.com/assets/bishun/animation/"
    gif_name=word.encode('unicode_escape')[-4:].decode('ascii')+"-bishun.gif"
    r=requests.get(url+gif_name)
    print("正在下载"+word+"的笔画动图")
    with open("D:/{0}.gif".format(word),'wb') as f:
        f.write(r.content)
        
if __name__=='__main__':
    word_list='你好骚啊'
    for word in word_list:
        get_gif(word)

本文地址:https://blog.csdn.net/Snow_224/article/details/107073106

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网