爬虫_简单下载一下网页

本示例演示了如何使用Python的urllib库抓取FreeBuf网站的文章链接,并将其保存为本地HTML文件。代码中包含了显示下载进度的功能。
import urllib
import string


#显示下载进度的函数
def callbackfunc(blocknum, blocksize, totalsize):
    '''回调函数
    @blocknum: 已经下载的数据块
    @blocksize: 数据块的大小
    @totalsize: 远程文件的大小
    '''
    percent = 100.0 * blocknum * blocksize / totalsize
    if percent > 100:
        percent = 100
    print "%.2f%%"% percent

#定义要抓取的页面
url = 'http://www.freebuf.com/articles'
#读取要抓取的页面
globalcontent = urllib.urlopen(url).read()
#捕捉文章列表
#这里在源码中查询"<dt><a href="这个字符串
#new_inner01_h = globalcontent.find('<li><a href=')
#new_inner01_h = globalcontent.find('<dt><a href=')

new_inner01_h = globalcontent.find('<div class="news-img"><a target="_blank" href=')
new_inner01_l = globalcontent.find('.html"><img calss=')

news_inner01 = globalcontent[new_inner01_h+47:new_inner01_l+5]

print news_inner01

local = 'd:\\crawl\\1.html'
urllib.urlretrieve(news_inner01, local, callbackfunc)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值