Estoy tratando de extraer datos del enlace Civic Commons Apps para mi proyecto. Puedo obtener los enlaces de la página que necesito. Pero cuando intento abrir los enlaces consigo "error urlopen [Errno -2] Nombre o servicio no conoce"Python Web Scraping - urlopen error [Errno -2] Nombre o servicio no conocido
La web raspado de código Python:
from bs4 import BeautifulSoup
from urlparse import urlparse, parse_qs
import re
import urllib2
import pdb
base_url = "http://civiccommons.org"
url = "http://civiccommons.org/apps"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
list_of_links = []
for link_tag in soup.findAll('a', href=re.compile('^/civic-function.*')):
string_temp_link = base_url+link_tag.get('href')
list_of_links.append(string_temp_link)
list_of_links = list(set(list_of_links))
list_of_next_pages = []
for categorized_apps_url in list_of_links:
categorized_apps_page = urllib2.urlopen(categorized_apps_url)
categorized_apps_soup = BeautifulSoup(categorized_apps_page.read())
last_page_tag = categorized_apps_soup.find('a', title="Go to last page")
if last_page_tag:
last_page_url = base_url+last_page_tag.get('href')
index_value = last_page_url.find("page=") + 5
base_url_for_next_page = last_page_url[:index_value]
for pageno in xrange(0, int(parse_qs(urlparse(last_page_url).query)['page'][0]) + 1):
list_of_next_pages.append(base_url_for_next_page+str(pageno))
else:
list_of_next_pages.append(categorized_apps_url)
me sale el siguiente error:
urllib2.urlopen(categorized_apps_url)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
¿Debo ocuparme de algo específico cuando realizo urlopen? Porque no veo un problema con los enlaces http que obtengo.
[editar] En segunda pasada me dieron el siguiente error:
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
El mismo código funciona muy bien en el Mac de mi amigo, pero falla en mi ubuntu 12.04.
También intenté ejecutar el código en la wiki de scrapper y terminó con éxito. Pero faltan algunas URL (en comparación con mac). ¿Hay alguna razón para este comportamiento?
¿Cuál es el valor de 'categorized_apps_url' en el punto del error? – kojiro
Además, sé que este tipo de comentario a menudo se considera molesto, pero puede encontrar la vida * mucho * más fácil si usa [httplib2] (http://code.google.com/p/httplib2/) o [solicitudes] (http://docs.python-requests.org/en/latest/index.html) en lugar de 'urllib2'. Ofrecen un conjunto más completo de funciones para trabajar con http. – kojiro
Su script funciona bien desde mi computadora como está. Me estoy ejecutando en una Mac con Python 2.7 y lo intenté con BeautifulSoup 3.2 y 4.0 y en ambos casos devuelve una lista de 69 enlaces principales y 117 enlaces de la página siguiente. Sospecho que es algo en su sistema que está bloqueando Python. ¿Has probado a hacer ping a esas URL directamente? ¿Quizás tiene un software antivirus que bloquea su script? –