I wanted to give my sister a mosaic puzzle with a snack (or a nice picture of food) on her birthday, so I explored it.
First need a software to make mosaic jigsaw puzzles, here use Foto-mosaik-edda (online also has a Web site, but I think this is more convenient, but also found a Chinese version, the address is http://witmax.cn/foto-mosaik-edda.html). To make mosaic puzzles, you need a database of images, at least thousands of images. So crawlers need to crawl.
After learning from the Internet after copy some code and then from a foreign image site crawled more than 4,000 keywords for food pictures, Python code as follows:
1 ImportRequests2 ImportRe3 ImportOS4 Import Time5 6 7 defget_url (URL):8KW = {'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64)'}9 Try:Tenr = Requests.get (URL, headers=kw) One r.raise_for_status () AR.encoding =r.apparent_encoding - returnR - except: the Print('wrong!!!!!!!!!!!') - - - defGet_photourl (photo_url): +KW = {'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64)'} - Try: +R = Requests.get (Photo_url, headers=kw) A r.raise_for_status () atR.encoding =r.apparent_encoding - returnR - except: - return 'wrong' - - in defget_photos (URL, new_fpath): -result =get_url (URL) to #pattern = Re.compile (R ' src= "https://images.pexels.com/photos/(\d+)/(. *?) \. (jpg|jpeg) \?auto=compress&cs=tinysrgb&h=350 "', Re. S) +Pattern =Re.compile ( - 'src= "https://images.pexels.com/photos/(\d+)/(. *?) \?auto=compress&cs=tinysrgb&h=750&w=1260 "') the #the true download link is static, not the images start * $Items =Re.findall (Pattern, Result.text)Panax Notoginseng - Print("log!"); the forIteminchItems: + Print(item); A the forIteminchItems: + Try: -Photo_url ='https://static.pexels.com/photos/'+ str (item[0]) +'/'+ str (item[1]) +"? auto=compress&cs=tinysrgb&h=350"; $ Print("URL:"+photo_url); $ #change the images in the image link to static - Save (Photo_url, item, New_fpath) -Time.sleep (1) the except: - ContinueWuyi the - defMakeDir (New_fpath, I, key): WuE =os.path.exists (New_fpath) - if notE: About os.makedirs (New_fpath) $ Os.chdir (New_fpath) - Print('folder'+ key +'_page'+ STR (i) +'Create success! ') - Else: - Print('folder already exists! ') A + the defSave (Photo_url, Item, New_fpath): -Final_fpath = New_fpath +'/'+ str (item[0]) + STR (item[1]); $ Print("Save file Name:"+Final_fpath) the the Print('Downloading pictures ...') the theresult =Get_photourl (Photo_url) - ifResult! ='wrong': in Print('Download Successful! ') the Else: the Print('failed') About theE =os.path.exists (Final_fpath) the the if notE: + Try: -With open (Final_fpath,'WB') as F: the f.write (result.content)Bayi except: the Print('Download failed! ') the Else: - Print('picture already exists') - the the defMain (): theKey = input ('Please enter search keywords:') the -URL ='https://www.pexels.com/search/'+ key +'/' the the #num = int (input (' Please enter total number of pages to download: ') # Default download starting from page 1th thest = Int (input ('Please enter the starting page number:'))94ed = int (Input ('Please enter the end page number:')) the theFpath ='C:/python/pic' the forIinchRange (St, ed+1):98New_fpath = Fpath +'/'+ key +'/'+ key +'_page'+Str (i) About MakeDir (New_fpath, I, key) -New_url = URL +'? page='+Str (i)101 Get_photos (New_url, New_fpath)102Time.sleep (3)103 104 theMain ()
Python crawler Code
Have to say that Python is really powerful, crawler really interesting, there is a source code in the Web page analysis and then deal with the joy of doing things ~
Use Python crawlers to crawl pictures and make mosaic puzzles