[Python-es] Reintentar

Oriol Andrés oriolandresmartin en gmail.com
Mie Ago 26 08:13:01 CEST 2015


hola, puedes encerrar la funcion en un bucle while.
while 1:
  response = urlopen
  if response.headers.code == 200:
     break
On Aug 26, 2015 2:02 a.m., "Gonzalo V" <gvm2121 en gmail.com> wrote:

> Hola a todos.
> Tengo un código que hace preguntas a un servidor y algunas veces retorna
> un 504, el codigo lo maneja bien pero me gustaría que reintentara.
> ¿Alguien me puede ayudar?
> el codigo es:
> import urllib.request
> import urllib.error
> from bs4 import BeautifulSoup
> import re, csv
> from FuncionCsv import LlenarCsv
>
>
>
> fhand=open('isbn.txt')
> #csvfile=open('ResultadoScrapping.csv', 'w', newline='')
> for line in fhand:
>     try:
>        req=urllib.request.urlopen('XXXXXXXX'+line)
>        resp=req.read()
>        soup=BeautifulSoup(resp,'html.parser')
>
>         origen=soup.find(string=re.compile("Origen:
> ")).find_next().get_text()
>         nombre=soup.find(name="h1",itemprop="name").get_text()
>         precioAhora=soup.find(name="p",class_="precioAhora").get_text()
>         d=soup.find(name="p",class_="stock").get_text()
>         disp=d.split()
>     except AttributeError:
>         disp="no encontrado"
>         nombre=''
>         origen=''
>         precioAhora=''
>     except urllib.error.HTTPError as e:
>             if e.getcode()==504:
>                 disp = "sin respuesta del servidor"
>                 print (e.getcode(),disp)
>                 csvfile.close()
>
>     print(line,nombre,origen,precioAhora,disp)
>     line1=line.split()
>     LlenarCsv('Resultado.csv',line1,nombre,origen,precioAhora,disp)
> cierracsv()
>
>
>
>
> Saludos,
> Gonzalo
>
> _______________________________________________
> Python-es mailing list
> Python-es en python.org
> https://mail.python.org/mailman/listinfo/python-es
> FAQ: http://python-es-faq.wikidot.com/
>
>
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://mail.python.org/pipermail/python-es/attachments/20150826/5e5d32b4/attachment.html>


Más información sobre la lista de distribución Python-es