#1 Le 30/05/2012, à 13:45
- k3c
[Script/python] Tester si des urls sur une page donnent une erreur 404
ou une autre
Un code Python minimaliste, demande Beautifulsoup 4 , prendre la version 4.0.5 à
http://www.crummy.com/software/BeautifulSoup/#Download
et urllib2 (inclus dans Python)
Pour éviter d'installer un soft supplémentaire juste pour cela
Le code affiche les liens http ou https qui renvoient une erreur (403,404, 500...)
Ce code avait été posté dans cette discussion
http://forum.ubuntu-fr.org/viewforum.php?id=35&p=2
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from urllib2 import Request, urlopen, URLError, HTTPError
import bs4 as BeautifulSoup
import sys
html = urlopen(sys.argv[1]).read()
soup = BeautifulSoup.BeautifulSoup(html)
for link in soup.find_all('a'):
href = link.get('href')
try:
if href[:5].lower() not in ('http:', 'https'):
href = sys.argv[1] + href
x = urlopen(href).read()
except HTTPError, e:
print "unable to open link %s code %s" % (href, e.code)
pass
Debian 12 sur Thinkpad reconditionné
Hors ligne