1. 程式人生 > >利用python爬蟲爬取圖片並且制作馬賽克拼圖

利用python爬蟲爬取圖片並且制作馬賽克拼圖

python爬蟲 splay ise 做事 c-c sea mage item -a

  想在妹子生日送妹子一張用零食(或者食物類好看的圖片)拼成的馬賽克拼圖,因此探索了一番= =。

  首先需要一個軟件來制作馬賽克拼圖,這裏使用Foto-Mosaik-Edda(網上也有在線制作的網站,但是我覺得這個比較方便,而且也找到了一個漢化過的版本,地址為http://witmax.cn/foto-mosaik-edda.html)。要制作馬賽克拼圖,需要一個圖片的數據庫,至少需要幾千張圖片。因此需要爬蟲來爬取。

  從網上學習了一番後copy了一些代碼然後從一個外國的圖片網站爬取了4000余張關鍵字為food的圖片,python代碼如下:

技術分享圖片
  1 import requests
  2 import
re 3 import os 4 import time 5 6 7 def get_url(url): 8 kw = {user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64)} 9 try: 10 r = requests.get(url, headers=kw) 11 r.raise_for_status() 12 r.encoding = r.apparent_encoding 13 return r 14 except
: 15 print(wrong!!!!!!!!!!!) 16 17 18 def get_photourl(photo_url): 19 kw = {user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64)} 20 try: 21 r = requests.get(photo_url, headers=kw) 22 r.raise_for_status() 23 r.encoding = r.apparent_encoding 24 return
r 25 except: 26 return wrong 27 28 29 def get_photos(url, new_fpath): 30 result = get_url(url) 31 # pattern = re.compile(r‘src="https://images.pexels.com/photos/(\d+)/(.*?)\.(jpg|jpeg)\?auto=compress&cs=tinysrgb&h=350"‘, re.S) 32 pattern = re.compile( 33 src="https://images.pexels.com/photos/(\d+)/(.*?)\?auto=compress&cs=tinysrgb&h=750&w=1260") 34 # 真正的下載鏈接是static,不是images開頭 35 36 items = re.findall(pattern, result.text) 37 38 print("log!"); 39 for item in items: 40 print(item); 41 42 for item in items: 43 try: 44 photo_url = https://static.pexels.com/photos/ + str(item[0]) + / + str(item[1]) + "?auto=compress&cs=tinysrgb&h=350"; 45 print("url: " + photo_url); 46 # 把圖片鏈接中的images,改成了static 47 save(photo_url, item, new_fpath) 48 time.sleep(1) 49 except: 50 continue 51 52 53 def makedir(new_fpath, i, key): 54 E = os.path.exists(new_fpath) 55 if not E: 56 os.makedirs(new_fpath) 57 os.chdir(new_fpath) 58 print(文件夾 + key + _page + str(i) + 創建成功!) 59 else: 60 print(文件夾已存在!) 61 62 63 def save(photo_url, item, new_fpath): 64 Final_fpath = new_fpath + / + str(item[0]) + str(item[1]); 65 print("保存文件名: " + Final_fpath) 66 67 print(正在下載圖片......) 68 69 result = get_photourl(photo_url) 70 if result != wrong: 71 print(下載成功!) 72 else: 73 print(失敗) 74 75 E = os.path.exists(Final_fpath) 76 77 if not E: 78 try: 79 with open(Final_fpath, wb) as f: 80 f.write(result.content) 81 except: 82 print(下載失敗!) 83 else: 84 print(圖片已存在) 85 86 87 def main(): 88 key = input(請輸入搜索關鍵詞(英文):) 89 90 url = https://www.pexels.com/search/ + key + / 91 92 # num = int(input(‘請輸入一共要下載的頁數:‘)) # 默認從第1頁開始下載 93 st = int(input(請輸入起始頁碼:)) 94 ed = int(input(請輸入終止頁碼:)) 95 96 fpath = C:/python/pic 97 for i in range(st, ed+1): 98 new_fpath = fpath + / + key + / + key + _page + str(i) 99 makedir(new_fpath, i, key) 100 new_url = url + ?page= + str(i) 101 get_photos(new_url, new_fpath) 102 time.sleep(3) 103 104 105 main()
python爬蟲代碼

  

  不得不說python真的很強大,爬蟲真的很有意思,有一種在網頁的源代碼中分析然後處理做事的快樂~

利用python爬蟲爬取圖片並且制作馬賽克拼圖