大虾帮我看下我这个脚本为什么不能创建文件?
本帖最后由 evandpr 于 2013-07-11 20:44:54 编辑
import urllib.request
import re,os
a = urllib.request.urlopen('http://www.daomubiji.com').read().decode('utf-8')
urls=re.findall('<a.*?href=(.*?\.html.*?)<\/a>',a,re.I)
for i in urls:
url = i.split('"')[1]
fname = url.split('/')[3]
#print(str(fname))
b = urllib.request.urlopen(url).read()
f = open(str(fname), 'w')
f.write(str(b))
f.close()
import urllib.request
import re,os
a = urllib.request.urlopen('http://www.daomubiji.com').read().decode('utf-8')
urls=re.findall('<a.*?href=(.*?\.html.*?)<\/a>',a,re.I)
for i in urls:
url = i.split('"')[1]
if url.split('/')[3]=='':
fname = url.split('/')[2]
else:
fname = url.split('/')[3]
#fname = url.split('/')[3]
#print(str(fname))
b = urllib.request.urlopen(url).read()
f = open(str(fname), 'w')
f.write(str(b))
f.close()