Python crawler Instance (vi) multi-process Download Jin Yong net novel

Source: Internet
Author: User
Tags xpath

Target task: Use multi-process to download each version of Jin Yong Net (old, revised, new revision) of the novel

The code is as follows:

#-*-coding:utf-8-*-ImportRequests fromlxmlImportetree fromMultiprocessingImportPoolImportOSImportsysreload (SYS) sys.setdefaultencoding ('Utf-8') Headers= {'user-agent':'mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) gecko/20100101 firefox/47.0'}defDownload (title,url, filename): Response= Requests.get (URL, headers=headers). Text HTML=etree. HTML (response) pages= Html.xpath ('//div//p/text ()') [2:] with open (filename,'a') as F:f.write (title+'\ n')     forPageinchPages:with open (filename,'a') as F:f.write (page+'\ n')defMain (URL): Start_url='http://www.jinyongwang.com'+URL sname= Start_url.split ('/') [-2]    ifSname.startswith ('o'): Folder='old/'        if( notos.path.exists (folder): Os.makedirs (folder)elifSname.startswith ('N'): Folder='new/'        if( notos.path.exists (folder): Os.makedirs (folder)Else: Folder='now/'        if( notos.path.exists (folder): Os.makedirs (folder) filename= folder+sname+'. txt'Base_url='http://www.jinyongwang.com'Response= Requests.get (Start_url, headers=headers). Text HTML=etree. HTML (response) URLs= Html.xpath ('//ul[@class = "Mlist"]/li/a/@href') Titles= Html.xpath ('//ul[@class = "Mlist"]/li//text ()')     forIndex,urlinchEnumerate (URLs): Full_url= base_url+URL title=Titles[index] Download (title, full_url, filename)if __name__=='__main__': Url01='http://www.jinyongwang.com/'Response= Requests.get (url01, headers=headers). Text HTML=etree. HTML (response) URLs= Html.xpath ('//li[@class = "Book_li"]/p[3]//a/@href') Pool=Pool () pool.map (Main,urls) pool.close () Pool.join ()

Results show:

Python crawler Instance (vi) multi-process Download Jin Yong net novel

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.