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.