#!/usr/bin/env python
#coding=utf-8
import threading,urllib2,HTMLParser,time,urllib
def download(url,file_name):
rfile = urllib.urlopen(url)
url=rfile.geturl()
urllib.urlretrieve(url,file_name+".pdf")
if __name__=="__main__":
#lParser = parseDownloadFiles()
content=urllib2.urlopen("http://www.taobaotest.com/notices/82").read()
start='<h2>Tcon</h2>'
end='<div class="article-nav">'
content=content[content.find(start):content.find(end)]
file_name_list=[]
url_list=[]
while 1:
download_line=content[content.find("<p>"):content.find("</p>")+len("</p>")]
if "down_link" in download_line:
seprate=unicode("、", "utf-8")
last=unicode(")", "utf-8")
spec=unicode("/", "utf-8")
if "<span" in download_line:
file_name=download_line[download_line.find('cboxElement">')+len('cboxElement">'):download_line.find('</span>')]
#if seprate in file_name:
#file_name=file_name[file_name.find(u"、"):]
else:
file_name=download_line[download_line.find('<p>')+len('<p>'):download_line.find('(<a')]
#file_name=(file_name[file_name.find(u"、"):]).strip()
file_name=unicode(file_name, "utf-8")
if seprate in file_name:
file_name=file_name[file_name.find(seprate)+len(seprate):]
if last in file_name:
file_name=file_name[:file_name.find(last)]
if spec in file_name:
file_name=file_name.replace(spec,"")
file_name_list.append(file_name)
#file_name_list.append(unicode(file_name, "utf-8"))
url=download_line[download_line.find('http://adc'):download_line.find('" target')]
url_list.append(url)
if content.find("</p>")==-1:
break
else:
content=content[content.find("</p>")+len("</p>"):]
for i in range(0,len(file_name_list)):
download(url_list[i],file_name_list[i])
转载于:https://my.oschina.net/u/2603916/blog/604123