1 ImportOS2 ImportRequests3 fromUrllib.parseImportUrlEncode4 fromHashlibImportMD55 fromMultiprocessing.poolImportPool6 7Group_start = 18Group_end = 59 Ten defget_page (offset): Oneparams = { A 'Offset': Offset, - 'format':'JSON', - 'keyword':'Street Pat', the 'AutoLoad':'true', - 'Count':' -', - 'Cur_tab':'3', - ' from':'Gallery', + } -URL ='https://www.toutiao.com/search_content/?'+UrlEncode (params) + Try: AResponse =requests.get (URL) at ifResponse.status_code = = 200: - returnResponse.json () - exceptrequests. Connectionerror: - returnNone - - defget_images (JSON): indata = Json.get ('Data') - ifData: to forIteminchData: + #print (item) -Image_list = Item.get ('image_list') thetitle = Item.get ('title') * #print (image_list) $ forImageinchimage_list:Panax Notoginseng yield { - 'Image': Image.get ('URL'), the 'title': Title + } A the defsave_image (item): + if notOs.path.exists (Item.get ('title')): -Os.mkdir (Item.get ('title')) $ Try: $Local_image_url = Item.get ('Image') -New_image_url = Local_image_url.replace ('List','Large') -Response = Requests.get ('http:'+New_image_url) the ifResponse.status_code = = 200: -File_path ='{0}/{1}. {2}'. Format (Item.get ('title'), MD5 (response.content). Hexdigest (),'jpg')Wuyi if notos.path.exists (file_path): theWith open (File_path,'WB') as F: - f.write (response.content) Wu Else: - Print('already downloaded', File_path) About exceptrequests. Connectionerror: $ Print('Failed to save image') - - defMain (offset): -JSON =get_page (offset) A forIteminchget_images (JSON): + Print(item) the save_image (item) - $ if __name__=='__main__': thePool =Pool () theGroups = ([x * 20 forXinchRange (Group_start, Group_end + 1)]) the Pool.map (Main, groups) the pool.close () -Pool.join ()
Analyze Ajax Crawl today's headline Street Photo gallery