有没有什么惊艳的微信头像?Python一键下载1000张!
- 2020 年 2 月 21 日
- 笔记
bee君一直以来,对选用一个合适的微信头像,感到比较头疼。
今天bee君为大家推荐1000多张,非常惊艳的能用做微信头像的图片,都已按照类别分好类,文末提供下载方法,相信大家一定能从中找到一款自己喜欢的。
1 requests失败
使用requests
请求微信表情图片网址https://www.zhihu.com/question/335825565
, 同时使用web网页提取包:lxml
,提取想要的所有图片链接地址,也就是下面的代码:
import requests from lxml import etree import pandas as pd import re url = 'https://www.zhihu.com/question/335825565' with requests.get(url) as res: content = res.content print(content) html = etree.HTML(content) imgs = html.xpath('//figure//img/@src') print(imgs)
这样直接运行,content的打印显示如下,并没有请求到想要的结果。
b'<html>rn<head><title>400 Bad Request</title></head>rn<body bgcolor="white">rn<center><h1>400 Bad Request</h1></center>rn<hr><center>openresty</center>rn</body>rn</html>rn'
2 直接提取
换种直接的方法,打开网页https://www.zhihu.com/question/335825565,F12查看网页结构:
直接把此标签的全部内容copy到本地,并提取出所有图片的地址。
选中上面标签,右键->copy->copy element 直接把此标签的全部内容copy到本地,保存为wechat_pic.html
下一步,提取出所有图片的地址,直接使用python的正则模块提取就行。
第一步:导入用到的包
import os import re from urllib.request import urlopen
第二步~最后一步:读入上面复制的标签内容,使用正则表达式提取出需要的图片url
,再使用urlopen
从这个url
下载图片文件,并保存到本地。这里面有一些细节问题,建议读者把代码下载后,跑跑试试。
with open('./data/wechat_pic.html',mode='r',errors='ignore') as f: lines = f.readlines() pic_urls = re.findall(r'<img src="(.*?).jpg"',lines[0]) pic_urls = [pic for pic in pic_urls if pic.startswith('https')] # 只保留以https开头的地址 #pic_urls = set(pic_urls) # 直接使用set会打破原来顺序,因为原网页按照图片分类,所以直接使用set去重,效果不佳 downloaded = [] for i,url in enumerate(pic_urls): if url in downloaded: continue print(f'{url}') q=urlopen(url+'.jpg') pic=q.read() save = open('./data/wechat_pic/%d.jpg'%(i,),mode='w+b') save.write(pic) q.close() save.close() print(f'pic{i} saved!') downloaded.append(url)
3下载图片
全部下载一共有图片1000多张,分为10几个类别,都能用做很惊艳的微信头像哦,自己挑选一张试试。
漂亮的小姐姐

部位放大图

动漫头像

好看的背景图

黑白图

简约风

表情头像

这是前186张图片的分类,其他800多张微信头像,请按照下面方式下载。
4 传送门
数据html文件,本文完整代码,下载到1000张微信表情图

统一下载方法:
1) 关注公众号
2) 并在这个号内回复:pic