0 Basic Writing Python crawler crawl Baidu paste code share _python

Source: Internet
Author: User

Here will not give everyone nonsense, directly on the code, the interpretation of the code in the comments inside, do not understand do not ask me to learn the basics to go!

Copy Code code as follows:

#-*-Coding:utf-8-*-
#---------------------------------------
# program: Baidu Paste the crawler
# Version: 0.1
# Author: why
# Date: 2013-05-14
# language: Python 2.7
# operation: Enter the address with the paging, remove the back number, set the starting page and end page.
# function: Download all pages in the corresponding page number and store them as HTML files.
#---------------------------------------
Import String, Urllib2
#定义百度函数
def Baidu_tieba (url,begin_page,end_page):
For I in range (Begin_page, end_page+1):
sname = String.zfill (i,5) + '. html ' #自动填充成六位的文件名
print ' is downloading the ' + str (i) + ' page and storing it as ' + sname + ' ... '
f = open (sname, ' w+ ')
m = urllib2.urlopen (url + str (i)). Read ()
F.write (M)
F.close ()
#--------Enter the parameters here------------------
# This is the address of a post in the Baidu bar of Shandong University
#bdurl = ' http://tieba.baidu.com/p/2296017831?pn= '
#iPostBegin = 1
#iPostEnd = 10

Bdurl = str (raw_input (U ' Please enter the address of post, remove the number after pn=: \ n '))
begin_page = Int (raw_input (U ' enter start page: \ n '))
end_page = Int (raw_input (U ' Please enter end page: \ n '))
#--------Enter the parameters here------------------
#调用
Baidu_tieba (Bdurl,begin_page,end_page)

The above is Python crawl Baidu bar a piece of simple code, very practical, you can expand their own.

Related Article

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.