Create a crawler collection novel using Python,

Source: Internet
Author: User

Create a crawler collection novel using Python,

Development Tool: python3.4
Operating System: win8
Main function: Specify the novel web page to crawl the novel directory, save it to the local location by chapter, and save the crawled web page to the local configuration file.
Crawled Website: http://www.cishuge.com/
Novel name: Liling night travel
Code Source: Self-signed

Import urllib. requestimport http. cookiejarimport socketimport timeimport retimeout = 20socket. setdefatimetimeout (timeout) sleep_download_time = 10time. sleep (sleep_download_time) def makeMyOpener (head = {'connection': 'Keep-alive', 'access': 'text/html, application/xhtml + xml ,*/*', 'Accept-color': 'en-US, en; q = 0.8, zh-Hans-CN; q = 0.5, zh-Hans; q = 100 ', 'User-agent': 'mozilla/5.0 (Windows NT 6.3; WOW64; T Rident/7.0; rv: 11.0) like Gecko '}): cj = http. cookiejar. cookieJar () opener = urllib. request. build_opener (urllib. request. HTTPCookieProcessor (cj) header = [] for key, value in head. items (): elem = (key, value) header. append (elem) opener. addheaders = header return opener def saveFile (save_path, txts): f_obj = open (save_path, 'W + ') for item in txts: f_obj.write (item +' \ n ') f_obj.close () # get_code_listco De_list = 'HTTP: // www.cishuge.com/read/0/771/'login = makeMyOpener () uop = login. open (code_list, timeout = 1000) data = uop. read (). decode ('gbk', 'ignore') pattern = re. compile ('<li> <a href = "(. *?) ". *?> (.*?) </A> </li> ', re. s) items = re. findall (pattern, data) print comment 'url _ r = open (url_path, 'R') url_arr = url_r.readlines (100000) url_r.close () print (len (url_arr )) url_file = open (url_path, 'A') print ('get downloaded website') for tmp in items: save_path = tmp [1]. replace (', '{}'.txt' url = code_list + tmp [0] if url + '\ n' in url_arr: continue print ('write log:' + url + '\ n ') url_file.write (url + '\ n') opene = makeMyOpen Er () op1 = opene. open (url, timeout = 1000) data = op1.read (). decode ('gbk', 'ignore') opene. close () pattern = re. compile ('(. *?) <Br/> ', re. S) txts = re. findall (pattern, data) saveFile (save_path, txts) url_file.close ()

Although the Code is still a bit flawed, I 'd like to share it with you and make improvements together.

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.