Tema: Problema
Ver Mensaje Individual
  #4 (permalink)  
Antiguo 02/11/2010, 14:46
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Problema

Código Python:
Ver original
  1. #Codigo en python 2.6
  2. import re
  3. import urllib
  4. import datetime
  5.  
  6. class AppURLopener(urllib.FancyURLopener):
  7.     version = "App/1.7"
  8.  
  9. urllib._urlopener = AppURLopener()
  10.  
  11. class Links():
  12.     def __init__(self,url):
  13.         self.__direccion=url
  14.  
  15.     def setter(self,url):
  16.         self.__direccion=url
  17.  
  18.     def adhtml(self):
  19.         try:
  20.             f= urllib.urlopen(self.__direccion)
  21.         except:
  22.             print("Fallo en", self.__direccion)
  23.             raise SystemExit
  24.         a=f.read()
  25.         f.close()
  26.         return a
  27.  
  28.  
  29.     def buscaVideo(self, html):
  30.         try:
  31.             Exprecion = re.compile('<a\s*href=[\'|"](.*?)[\'|"]')
  32.             finalVideo=[]
  33.             ec = Exprecion.findall(str(html))
  34.             for i in range(0,len(ec)):
  35.                 if (re.match("[0-9]*",ec[i])):
  36.                     finalVideo.append(ec[i])
  37.  
  38.         except:
  39.             print("ocurrio un error")
  40.         return (finalVideo)
  41.  
  42.  
  43.     def buscaDescripcion(self, html):
  44.         Exprecion = re.compile('<div id="description">(.*?)<br><br>(.*?)<br><br>(.*?)<b r><br>(.*?)<br><br>(.*?)<br><br>(.*?) </div>')
  45.         ec = Exprecion.findall(str(html))
  46.         return (ec)
  47.  
  48.     def buscaRate(self,html):
  49.         Exprecion = re.compile('<td>(.*?)</td>')
  50.         ec = Exprecion.findall(str(html))
  51.         return (ec)
  52.  
  53.     def buscaTitulo(self,html):
  54.         Exprecion = re.compile('<div class="title">(.*?)</div>')
  55.         ec = Exprecion.findall(str(html))
  56.         return (ec)
  57.    
  58.     def buscaCategoria(self,html):
  59.         Exprecion = re.compile('<p id="eow-category"><a href="/.*">(.*?)</a></p>')
  60.         ec = Exprecion.findall(str(html))
  61.         return (ec)
  62.  
  63. url='vimeo.com/ajax/user/home_videos?&jdata={"page":1}'
  64. link=Links(url)
  65. fecha=datetime.date.today()
  66. dia=(str(fecha))
  67. print(fecha)
  68. loco=open("videosVimeo.txt","w")
  69.  
  70.  
  71. loco.write("<?xml>\n")
  72. loco.write('<list origin="vimeo" date="'+ dia +'">\n')
  73. for k in range(1,35):
  74.     url='http://vimeo.com/ajax/user/home_videos?&jdata={"page":'+str(k)+'}'
  75.     print(url)
  76.     print (url.__repr__())
  77.     link.setter(url)
  78.     html=link.adhtml()
  79.     print html
  80.     arrayVideo=link.buscaVideo(html)
  81.     s=set(arrayVideo)
  82.     for h in s:
  83.         link.setter("http://www.vimeo.com"+h)
  84.         html=link.adhtml()
  85.         a=link.buscaDescripcion(html)
  86.         rate=link.buscaRate(html)
  87.         titulo=link.buscaTitulo(html)
  88.         loco.write('\t<item>\n')
  89.         loco.write('\t\t <title>'+ titulo[0] + '</title>\n')
  90.         loco.write('\t\t<refer>http://www.vimeo.com"'+h+'"</refer>\n')
  91.         loco.write('\t\t<rate>'+ rate[1] +'</rate>\n')
  92.         loco.write('\t\t<description>' +a[2]+ '</description>\n')
  93.         #loco.write('\t\t<category>'+categoria[0]+'</category>\n')
  94.         loco.write('\t</item>\n')
  95.  
  96. loco.close()
  97.  
  98. print("Aca salen los videos")
  99. for j in range(1,len(arrayVideo)):
  100.     print(arrayVideo[j])
Al parecer urllib de python 3.1 no hace muchas cosas por defecto que el de python 2.6 Si
Por cierto tienes problemas con los indices de tus listas. Por eso entre otras cosas es mejor usar un parser.

Codigo en python 3.1
Código Python:
Ver original
  1. import re
  2. import urllib.request
  3. import datetime
  4.  
  5. class Links():
  6.     def __init__(self,url):
  7.         self.__direccion=url
  8.  
  9.     def setter(self,url):
  10.         self.__direccion=url
  11.  
  12.     def adhtml(self):
  13.         user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
  14.         values = {}
  15.         headers = {'User-Agent':user_agent}
  16.         data = urllib.parse.urlencode(values)
  17.         req = urllib.request.Request(url, data, headers)
  18.         f= urllib.request.urlopen(req)
  19.         a=f.read()
  20.         f.close()
  21.         return a
  22.  
  23.  
  24.     def buscaVideo(self, html):
  25.         try:
  26.             Exprecion = re.compile('<a\s*href=[\'|"](.*?)[\'|"]')
  27.             finalVideo=[]
  28.             ec = Exprecion.findall(str(html))
  29.             for i in range(0,len(ec)):
  30.                 if (re.match("[0-9]*",ec[i])):
  31.                     finalVideo.append(ec[i])
  32.  
  33.         except:
  34.             print("ocurrio un error")
  35.         return (finalVideo)
  36.  
  37.  
  38.     def buscaDescripcion(self, html):
  39.         Exprecion = re.compile('<div id="description">(.*?)<br><br>(.*?)<br><br>(.*?)<b r><br>(.*?)<br><br>(.*?)<br><br>(.*?) </div>')
  40.         ec = Exprecion.findall(str(html))
  41.         return (ec)
  42.  
  43.     def buscaRate(self,html):
  44.         Exprecion = re.compile('<td>(.*?)</td>')
  45.         ec = Exprecion.findall(str(html))
  46.         return (ec)
  47.  
  48.     def buscaTitulo(self,html):
  49.         Exprecion = re.compile('<div class="title">(.*?)</div>')
  50.         ec = Exprecion.findall(str(html))
  51.         return (ec)
  52.    
  53.     def buscaCategoria(self,html):
  54.         Exprecion = re.compile('<p id="eow-category"><a href="/.*">(.*?)</a></p>')
  55.         ec = Exprecion.findall(str(html))
  56.         return (ec)
  57.  
  58. url='vimeo.com/ajax/user/home_videos?&jdata={"page":1}'
  59. link=Links(url)
  60. fecha=datetime.date.today()
  61. dia=(str(fecha))
  62. print(fecha)
  63. loco=open("videosVimeo.txt","w")
  64.  
  65.  
  66. loco.write("<?xml>\n")
  67. loco.write('<list origin="vimeo" date="'+ dia +'">\n')
  68. for k in range(1,35):
  69.     url='http://vimeo.com/ajax/user/home_videos?&jdata={"page":'+str(k)+'}'
  70.     print(url)
  71.     print (url.__repr__())
  72.     link.setter(url)
  73.     html=link.adhtml()
  74.     arrayVideo=link.buscaVideo(html)
  75.     s=set(arrayVideo)
  76.     for h in s:
  77.         link.setter("http://www.vimeo.com"+h)
  78.         html=link.adhtml()
  79.         a=link.buscaDescripcion(html)
  80.         rate=link.buscaRate(html)
  81.         titulo=link.buscaTitulo(html)
  82.         loco.write('\t<item>\n')
  83.         if titulo:
  84.             loco.write('\t\t <title>'+ titulo[0] + '</title>\n')
  85.         loco.write('\t\t<refer>http://www.vimeo.com"'+h+'"</refer>\n')
  86.         if len(titulo) > 2:
  87.             loco.write('\t\t<rate>'+ rate[1] +'</rate>\n')
  88.         if len(titulo) > 3:
  89.             loco.write('\t\t<description>' +a[2]+ '</description>\n')
  90.         #loco.write('\t\t<category>'+categoria[0]+'</category>\n')
  91.         loco.write('\t</item>\n')
  92.  
  93. loco.close()
  94.  
  95. print("Aca salen los videos")
  96. for j in range(1,len(arrayVideo)):
  97.     print(arrayVideo[j])
No se si esta bien la identación