Analyze Ajax Crawl today's headline Street Photo gallery

Source: Internet
Author: User
Tags urlencode

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.