简单的PYTHON应用(使用了urllib, re等库)

下面是一个简单的PYTHON应用,主要 是使用了python的urllib,re等库,非常简单,可以作为其他的python在网络方面应用的模板(使用Python3测试)

#!/usr/bin/env python
import sys
import re
import urllib.request
from urllib.parse import urlparse
def download(url,flag):
    try:
        fd=urllib.request.urlopen(url) # Open the URL and get the file description
        page=fd.read() # Get the index page html content
        unicodePage=page.decode('gb2312') # Get the unicode page html content. Can display chinese character
        tempURL=urlparse(url)
        tempURL=tempURL.geturl()
        tempLIST=tempURL.split('/')
        fileName=tempLIST[-1] ##Get the file name via URL
        path=tempURL[0:tempURL.index(fileName)] ## get the path info
        print("Downloading: ",tempURL,";Saving: ",fileName)
   
        writefd=open(fileName,'w') ## get the write file description
        writefd.write(unicodePage) ## write to the file
        writefd.close()
    except:
        pass
   
    if flag==1: # flag==1 shows that the page is the index page
        """
        first get the url list
        then call download to download the url and saving the html to file
        """
        pattern=r'a href="([^"]+)"'
        linklist=re.findall(pattern,unicodePage)
       
        for item in linklist:
            if not item.startswith('http'):
                temp=path+item.strip()
                print("!!!!....",temp)
                download(temp,0)
if len(sys.argv)
url=sys.argv[1] # Get the URL address
download(url,1)
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值