Skip to content

Instantly share code, notes, and snippets.

@henriquegogo
Created March 3, 2011 12:01
Show Gist options
  • Save henriquegogo/852667 to your computer and use it in GitHub Desktop.
Save henriquegogo/852667 to your computer and use it in GitHub Desktop.
Script que baixa todas as fotos do SXC
#!/usr/bin/env phyton
#-*- encoding: utf8 -*-
'''
Esse programa vai baixar todas as imagens do SXC
e construir um banco de dados com as TAGs de cada
imagem.
'''
import urllib
def logar():
'''
Permite que se logue para poder baixar todas as imagens em alta definição
'''
def baixar(id):
pagina = show()
go("http://www.sxc.hu%s" % pagina.split('img src="')[1].split('" alt')[0])
save_html(id + '.jpg')
print "Baixando " + id + ".jpg"
def totalPaginas():
'''
Pega o número do total de páginas
'''
f = urllib.urlopen("http://www.sxc.hu/category/9002/")
contents = f.read()
f.close()
ache = contents.find("PAGE 1 / ")
if ache != -1:
inicio = ache + 9
numero = contents[inicio:].split('<')
return numero[0]
def tagsImg(id):
'''
Capta o nome e as TAGs da imagem
'''
import re
f = urllib.urlopen("http://www.sxc.hu/photo/" + id)
for line in f:
if '<h1>Stock photo: ' in line:
dadosImg = re.sub('</?[^>]*>', '', line).split(' ')
idImg = dadosImg[-9]
nomeImg = ' '.join(dadosImg[2:-10])
print "O id é: ", idImg
print "O nome é: ", nomeImg
if '<a href="browse.phtml?f=search&w=1&txt=' in line:
tags = re.sub('</?[^>]*>', '', line).strip('Keywords ').strip('\n ').split(' ')
print "As tags são: ", tags
f.close()
def idImg(pag):
'''
Lê o arquivo e pega apenas o número das imagens
'''
f = urllib.urlopen("http://www.sxc.hu/category/9002/" + str(pag))
for line in f:
if '<a href="photo/' in line:
ache = line.find('<a href="photo/')
if ache != -1:
inicio = ache + 15
numero = line[inicio:].split('"')
tagsImg(numero[0])
baixar(numero[0])
f.close()
# Vamos chamar as funções para começar o programa
# logar()
from twill.commands import *
go("http://www.sxc.hu/")
formvalue("3", "login", "usuario")
formvalue("3", "pass", "senha")
submit()
for x in range(1): #in totalPaginas()
idImg(x)
@rogerco
Copy link

rogerco commented Aug 12, 2013

Olá Henrique,

Você sabe dizer se este script ainda funciona? (hoje é 11/8/2013) Estou tentando usá-lo porém não funciona. Estou rodando-o no Python com a biblioteca Twill instalada, e a mensagem final é:

Traceback (most recent call last):
File "C:\Users\Roger\Desktop\sxc\script\sxcdown.py", line 83, in
idImg(x)
File "C:\Users\Roger\Desktop\sxc\script\sxcdown.py", line 68, in idImg
baixar(numero[0])
File "C:\Users\Roger\Desktop\sxc\script\sxcdown.py", line 19, in baixar
go("http://www.sxc.hu%s" % pagina.split('img src="')[1].split('" alt')[0])
File "C:\Program Files (x86)\Python27\lib\twill\commands.py", line 112, in go
browser.go(url)
File "C:\Program Files (x86)\Python27\lib\twill\browser.py", line 122, in go
raise BrowserStateError("cannot go to '%s'" % (url,))
BrowserStateError: cannot go to 'http://www.sxc.huimg/stock_xchng.gif" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment