您的位置:首页 > 编程语言 > Python开发

python实现网站的js文件下载

2014-06-18 01:20 656 查看
import urllib2

import os

import re

import shutil

if not os.path.exists('jsfiles'):

os.mkdir("jsfiles")

url='http://cn.dolphin.com'

html=urllib2.urlopen(url).read()

js_url_pattern=r'.+?src="(\S+)"'

r=re.compile(r'<script[^>].*?</script>',re.DOTALL)

temp=[1]

for js in r.findall(html):

temp = re.findall(js_url_pattern,js)

if temp:

js_url=temp[0]

strs=js_url.split('/')

filename=strs[len(strs)-1]

f=urllib2.urlopen(url)

if not os.path.exists("jsfiles/"+filename):

with open(filename,'wb') as code:

code.write(f.read())

shutil.move(filename,'jsfiles')

print 'done'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: