![]() |
|
How to download images using urllib - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: How to download images using urllib (/Thread-How-to-download-images-using-urllib) Pages:
1
2
|
How to download images using urllib - Harvey - 04-07-2013 Quick little tutorial on using the urllib module to download images from sites. Code: import urllib
urllib.URLopener().retrieve('http://i.imgur.com/ZHGAYQw.jpg', 'output.jpg')Pretty simple stuff. Firstly, we need to import the module (first line). Secondly, we call URLopener() from within our module and use the retrieve() function. Retrieve takes two parameters, the URL of the image you want to download, and the filename you want to save the image with when it's downloaded to your computer. Pretty easy stuff hey? You can do a lot with modified versions of this code. I created an Imgur scraper that downloads random images from the site using only a few lines of code. Got to love python. :p RE: [Python] How to download images using urllib - Greed - 04-07-2013 Lovely tutorial mate, can you teach us how to edit RPG sources? RE: [Python] How to download images using urllib - Harvey - 04-07-2013 (04-07-2013, 01:53 AM)Greed Wrote: Lovely tutorial mate, can you teach us how to edit RPG sources? RPG's such as? :p RE: [Python] How to download images using urllib - Greed - 04-07-2013 Code: from random import randint
class Character:
def __init__(self):
self.name = ""
self.health = 1
self.health_max = 1
def do_damage(self, enemy):
damage = min(
max(randint(0, self.health) - randint(0, enemy.health), 0),
enemy.health)
enemy.health = enemy.health - damage
if damage == 0: print "%s evades %s's attack." % (enemy.name, self.name)
else: print "%s hurts %s!" % (self.name, enemy.name)
return enemy.health <= 0
class Enemy(Character):
def __init__(self, player):
Character.__init__(self)
self.name = 'a goblin'
self.health = randint(1, player.health)
class Player(Character):
def __init__(self):
Character.__init__(self)
self.state = 'normal'
self.health = 10
self.health_max = 10
def quit(self):
print "%s can't find the way back home, and dies of starvation.\nR.I.P." % self.name
self.health = 0
def help(self): print Commands.keys()
def status(self): print "%s's health: %d/%d" % (self.name, self.health, self.health_max)
def tired(self):
print "%s feels tired." % self.name
self.health = max(1, self.health - 1)
def rest(self):
if self.state != 'normal': print "%s can't rest now!" % self.name; self.enemy_attacks()
else:
print "%s rests." % self.name
if randint(0, 1):
self.enemy = Enemy(self)
print "%s is rudely awakened by %s!" % (self.name, self.enemy.name)
self.state = 'fight'
self.enemy_attacks()
else:
if self.health < self.health_max:
self.health = self.health + 1
else: print "%s slept too much." % self.name; self.health = self.health - 1
def explore(self):
if self.state != 'normal':
print "%s is too busy right now!" % self.name
self.enemy_attacks()
else:
print "%s explores a twisty passage." % self.name
if randint(0, 1):
self.enemy = Enemy(self)
print "%s encounters %s!" % (self.name, self.enemy.name)
self.state = 'fight'
else:
if randint(0, 1): self.tired()
def flee(self):
if self.state != 'fight': print "%s runs in circles for a while." % self.name; self.tired()
else:
if randint(1, self.health + 5) > randint(1, self.enemy.health):
print "%s flees from %s." % (self.name, self.enemy.name)
self.enemy = None
self.state = 'normal'
else: print "%s couldn't escape from %s!" % (self.name, self.enemy.name); self.enemy_attacks()
def attack(self):
if self.state != 'fight': print "%s swats the air, without notable results." % self.name; self.tired()
else:
if self.do_damage(self.enemy):
print "%s executes %s!" % (self.name, self.enemy.name)
self.enemy = None
self.state = 'normal'
if randint(0, self.health) < 10:
self.health = self.health + 1
self.health_max = self.health_max + 1
print "%s feels stronger!" % self.name
else: self.enemy_attacks()
def enemy_attacks(self):
if self.enemy.do_damage(self): print "%s was slaughtered by %s!!!\nR.I.P." %(self.name, self.enemy.name)
Commands = {
'quit': Player.quit,
'help': Player.help,
'status': Player.status,
'rest': Player.rest,
'explore': Player.explore,
'flee': Player.flee,
'attack': Player.attack,
}
p = Player()
p.name = raw_input("What is your character's name? ")
print "(type help to get a list of actions)\n"
print "%s enters a dark cave, searching for adventure." % p.name
while(p.health > 0):
line = raw_input("> ")
args = line.split()
if len(args) > 0:
commandFound = False
for c in Commands.keys():
if args[0] == c[:len(args[0])]:
Commands[c](p)
commandFound = True
break
if not commandFound:
print "%s doesn't understand the suggestion." % p.nameRE: [Python] How to download images using urllib - Harvey - 04-07-2013 What game is this and what would you like modified? :p RE: [Python] How to download images using urllib - Greed - 04-07-2013 (04-07-2013, 01:58 AM).py Wrote: What game is this and what would you like modified? :p Not sure i just pasted that on my notepad, What can you make with it? RE: [Python] How to download images using urllib - Harvey - 04-07-2013 (04-07-2013, 01:59 AM)Greed Wrote:(04-07-2013, 01:58 AM).py Wrote: What game is this and what would you like modified? :p It's a basic text-based game. If you save it as a .py file, you should be able to run it and see what it does. Pretty crappy game if you ask me though. :p RE: [Python] How to download images using urllib - Greed - 04-07-2013 (04-07-2013, 02:01 AM).py Wrote:(04-07-2013, 01:59 AM)Greed Wrote:(04-07-2013, 01:58 AM).py Wrote: What game is this and what would you like modified? :p Lol, Can you compile it? I wanna see RE: [Python] How to download images using urllib - Harvey - 04-07-2013 (04-07-2013, 02:01 AM)Greed Wrote:(04-07-2013, 02:01 AM).py Wrote:(04-07-2013, 01:59 AM)Greed Wrote:(04-07-2013, 01:58 AM).py Wrote: What game is this and what would you like modified? :p Yeah, it compiled and worked for me. It's very unintuitive and pretty poorly done though. RE: [Python] How to download images using urllib - Greed - 04-07-2013 (04-07-2013, 02:49 AM).py Wrote:(04-07-2013, 02:01 AM)Greed Wrote:(04-07-2013, 02:01 AM).py Wrote:(04-07-2013, 01:59 AM)Greed Wrote:(04-07-2013, 01:58 AM).py Wrote: What game is this and what would you like modified? :p Can you send it to me? |