Multi-threaded Crawler

Source: Internet
Author: User

1 #Multi-threaded crawler2 #use of the map function3 #From multiprocessing.dummy import Pool4 #Pool=pool (4)5 #results = Pool.map (crawl function, url list)6 #Example Demo:7  fromMultiprocessing.dummyImportPool as ThreadPool8 ImportRequests9 Import TimeTen  One defgetsource (URL): AHTML =requests.get (URL) -  -URLs = [] the  -  forIinchRange (1,21): -NewPage ='http://tieba.baidu.com/p/3522395718?pn='+Str (i) - urls.append (newpage) +  -Time1 =time.time () +  A  forIinchURLs: at     Print(i) - GetSource (i) -Time2=time.time () - Print('single thread time consuming:'+ STR (time2-time1)) -  - #Enable multithreading inPool = ThreadPool (4) -time3=time.time () toResults =Pool.map (getsource,urls) + pool.close () - Pool.join () theTime4 =time.time () * Print('Parallel time consuming:'+ STR (time4-time3)) $ Panax Notoginseng #Output Result: - #Single thread Time: 20.18715476989746 the #concurrent Time: 5.100291728973389

Multi-threaded Crawler

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.