Python writes Baidu post-paste simple crawler

Source: Internet
Author: User
Action: Enter the address with paging, remove the last number, set the start and end pages

function: Download all pages corresponding to the page number and save as HTML file, named at the current time

Code:

#-*-Coding:utf-8-*-#----------------------------#  Program: Baidu Paste the small reptile #  Date: 2015/03/28#  language: Python 2.7#  Action: Enter the address with paging, remove the last number, set the starting and ending pages #  function: Download all pages corresponding to the page number and save as an HTML file, name the current time #----------------------------Import Urllib2import time def baidu_tieba (URL, start, end): For  I in range (Start, end):    sName = Time.strftime ('%y%m%d%h% m%s ') + str (i) + '. html '    print ' is downloading the section ' + str (i) + ' page and save it as ' + SName + ' ... '    f = open (SName, ' w+ ')    m = Urlli B2.urlopen (Url+str (i))    n = m.read ()    f.write (n)    f.close ()  print ' successful download '     Baiduurl = str (raw_ Input (' Please enter the address of the post, remove the number >>\n ' behind the pn ') begin_page = Int (raw_input (' Please enter the starting page number >>\n ') end_page = Int (Raw_input ( ' Please enter the end page number of the post >>\n ')         Baidu_tieba (Baiduurl, Begin_page, End_page)

The above is the whole content of this article, I hope we can learn python to make a crawler to help.

  • 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.