A simple crawler for writing Baidu Post bars in Python

Source: Internet
Author: User
This article mainly introduces the simple crawler for compiling Baidu Post Bar in Python, and simply downloads the corresponding page number and saves it as an html file named after the current time. here, I will share it with you. Operation: enter a paging address, remove the last number, and set the start and end pages.

Function: Download all pages of the corresponding page number and save them as HTML files, named after the current time.

Code:

#-*-Coding: UTF-8-*-# -------------------------- # Program: Crawlers of Baidu Post bars # Date: # Language: Python 2.7 # operation: enter the address with pagination, remove the last number and set the start and end pages # function: Download all pages on the corresponding page and save them as HTML files, name it at 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' downloading '+ str (I) + 'webpage, and save it as '+ sName + '... 'F = open (sName, 'W + ') m = urllib2.urlopen (url + str (I) n = m. read () f. write (n) f. close () print 'successfully downloaded 'baiduurl = str (raw_input (' enter the address of the post, remove the number after pn> \ n ')) begin_page = int (raw_input ('Enter the starting page number of the Post> \ n') end_page = int (raw_input ('Enter the ending page number of the Post> \ n ')) baidu_tieba (baiduurl, begin_page, end_page)

The above is all the content of this article, hoping to help you learn how to create a crawler using Python.

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.