有沒有什麼驚艷的微信頭像?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