Vote count:
0
I made a small program that gets a website and fetches the images and save them in a directory.
If I run the script it works fine, but the program freezes until it downloads every image on the website.
Here's part of the code.
def download(self):
# gets the link
thread = self.textbox_thread.text()
try:
page = urllib2.urlopen(thread)
except urllib2.HTTPError:
print 'wrong thread ID'
# parse the HTML
soup = BeautifulSoup(page)
soup = soup.body.find_all('div', attrs={'class':'postContainer replyContainer'})
links = []
for n in soup:
test = n.find('div', attrs={'class':'fileText'})
if test != None:
link = test.find('a', href=True).get('href')
links.append(link)
for i, link in enumerate(links):
# set the progress bar percentage
value = int((i/ float(len(links)-1)*100))
self.progressBar.setValue(value)
# downloads the image
file = urllib2.urlopen('http:' + link)
# set the filename properly
board_thread = thread.replace("/","_").replace('thread',"").split("org")[1]
name_of_file = board_thread.replace("__","_")[1:] + "_%04d" % (i + 1) + ".jpg"
# saves the file
complete_path = os.path.abspath(self.textbox_path.text()+name_of_file)
output = open(complete_path, 'wb')
output.write(file.read())
output.close()
asked 20 secs ago
Program not responding but working in background
Aucun commentaire:
Enregistrer un commentaire