首页 > 编程 > Python > 正文

python自学-第八次作业

2019-11-08 02:39:18
字体:
来源:转载
供稿:网友
#coding=utf8import osimport urllibimport random#获取地址,然后写入文件def save_url_content(url,folder_path): if not (url.startswith('http://') and url.startswith('https://')): PRint u'error' if not os.path.isdir(folder_path): return u'folder_path not a folder' d = urllib.urlopen(url) content = d.read() print content random_name = 'test_%s.txt' % random.randint(1,1000) #filepath = '%s%s' %(folder_path,random_name) filepath = os.path.join(folder_path,random_name) file_handle = open(filepath,'w') file_handle.write(content) file_handle.close() return filepath#print save_url_content('aa','dfsf')#print save_url_content('http://www.baidu.com','fdsfsd')print save_url_content('http://www.baidu.com','F://')#获取url中的连接数量def get_url_list(url): if not (url.startswith('http://') and url.startswith('https://')): print u'error' d = urllib.urlopen(url) content = d.read() print content return len(content.split('<a href=')) -1print get_url_list("http://www.baidu.com")#递归解决目录下的所有文件,只能用递归来解决,否则需要用栈来记忆递归过程
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表