python下载

 

自动下载Dive into Python 3网页及其相关链接

# -*- coding=utf-8 -*-
import os
import urllib
import re

#1. 下载种子;从指定种子网页开始自动下载,递归下载有效链接
source_link = 'http://woodpecker.org.cn/diveintopython/toc/index.html'
f = open(source_link.split("/")[-1], 'w')
print 'save to file : ', os.getcwd()

page = urllib.urlopen(source_link)
page_content = page.read()

#找到有效链接
# . 匹配任意除换行符外的字符
# * 匹配前一个字符0次到无限次
# + 匹配前一个字符1次到无限次
# [] 对应位置可以是字符集中的任意字符
# [^] 对应位置不是字符集中的任意字符
m = re.findall(r'<a href="([^"#]+)".*>.*</a>', page_content)
mm = []
for mi in m:
    if (mi not in mm) and (mi.endswith('html')):
        mm.append(mi)

f.write(page_content)
f.close()

#2. 下载有效链接页面
print "%d pages to be downloaded "%(len(mm))
for mmi in mm:
    #os.path.dirname("a/b/c.txt") ==> a/b
    #os.path.basename("a/b/c.txt") ==> c.txt
    sub_link = os.path.dirname(source_link) + "/" + mmi
    f = open(sub_link.split("/")[-1], 'w')
    
    page = urllib.urlopen(sub_link)
    page_content = page.read()

    f.write(page_content)
    f.close()
    print '.',
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值