python爬虫教程之爬取百度贴吧并下载的示例

yipeiwu_com7年前Python爬虫

测试url:http://tieba.baidu.com/p/27141123322?pn=
begin  1
end   4

复制代码 代码如下:

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 '正在下载第' + str(i) + '个网页,并将其存储为' + sName + '..........'
        f = open (sName,'w+')
        m = urllib2.urlopen(url + str(i)).read()
        f.write(m)
        f.close()


bdurl = str(raw_input('url:  \n'))
begin_page = int(raw_input('begin :\n'))
end_page = int(raw_input('end : \n'))

baidu_tieba(bdurl,begin_page,end_page)

相关文章

python爬虫自动创建文件夹的功能

该爬虫应用了创建文件夹的功能: #file setting folder_path = "D:/spider_things/2016.4.6/" + file_name +"/" i...

python 每天如何定时启动爬虫任务(实现方法分享)

python2.7环境下运行 安装相关模块 想要每天定时启动,最好是把程序放在linux服务器上运行,毕竟linux可以不用关机,即定时任务一直存活; #coding:utf8 im...

Python爬虫通过替换http request header来欺骗浏览器实现登录功能

Python爬虫通过替换http request header来欺骗浏览器实现登录功能

以豆瓣为例,访问https://www.douban.com/contacts/list 来查看自己关注的人,要登录才能查看。 如果用requests.get()方法获取这个http,没...

Python网络爬虫之爬取微博热搜

Python网络爬虫之爬取微博热搜

微博热搜的爬取较为简单,我只是用了lxml和requests两个库 url= https://s.weibo.com/top/summary?Refer=top_hot&topnav=1...

Python使用Mechanize模块编写爬虫的要点解析

 mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面。结合beautifulsoup和re模块,可以有效的解析web...