Problem mit httplib

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
doca82
User
Beiträge: 48
Registriert: Mittwoch 16. September 2009, 19:39
Wohnort: Berlin

Hallo,
benutze python 2.6 und habe folgendes Problem mit dem folgenden Code um Links auslesen:

Code: Alles auswählen

from httplib import *
import re


def getLinks(url):
    conn = httplib.HTTPConnection(url)
    conn.request('GET' , '/')
    antw = conn.getresponse()
    html = antw.read()
    
    p = re.compile('(?<=href=")(.*?)(?=")')
    link_matches = p.findall(html)
    return link_matches

print getLinks('www.touristiklinks.de/')
Traceback (most recent call last):
File "C:\...", line 8, in <module>
conn = httplib.HTTPConnection(url)
NameError: name 'httplib' is not defined

Das Modul ist als Standartmodul vorhanden, soweit ich weiß?!
Als ziemlicher Anfänger hab ich wirklich keinen Blassen...

ohne die Basisklassenbenennung vor HTTPConnection(url)
also "conn = HTTPConnection(url)"

bekomm ich folgende Fehlermeldungen:

Traceback (most recent call last):
File "C..." line 20, in <module>
print getLinks('www.touristiklinks.de/')
File "...", line 12, in getLinks
conn.request('GET' , '/')
File "C:\Python26\lib\httplib.py", line 874, in request
self._send_request(method, url, body, headers)
File "C:\Python26\lib\httplib.py", line 911, in _send_request
self.endheaders()
File "C:\Python26\lib\httplib.py", line 868, in endheaders
self._send_output()
File "C:\Python26\lib\httplib.py", line 740, in _send_output
self.send(msg)
File "C:\Python26\lib\httplib.py", line 699, in send
self.connect()
File "C:\Python26\lib\httplib.py", line 683, in connect
self.timeout)
File "C:\Python26\lib\socket.py", line 498, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed


Vielleicht kann mir jemand helfen...
ist es der falsche http port?
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5555
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Oberhofen im Inntal (Tirol)
Kontaktdaten:

Hallo doca82!

Code: Alles auswählen

import httplib
mfg
Gerold
:-)

PS: Probiere es mal mit der Zusätzlichen Angabe des Protokolls: "http://www.touristiklinks.de". Vielleicht hilft das.

.
http://halvar.at | Kleiner Bascom AVR Kurs
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5555
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Oberhofen im Inntal (Tirol)
Kontaktdaten:

Nachtrag:

Das mit dem Herunterladen kannst du aber auch einfacher haben:

Code: Alles auswählen

>>> import urllib2
>>> response = urllib2.urlopen("http://halvar.at/")
>>> response.read(100)
'<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN'
>>> 
mfg
Gerold
:-)
http://halvar.at | Kleiner Bascom AVR Kurs
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
doca82
User
Beiträge: 48
Registriert: Mittwoch 16. September 2009, 19:39
Wohnort: Berlin

hi ja *fass an die stirn* das mit dem "from" steht in meinem Buch ;), dass allerdings schon älter ist...

Deine Variante ist unproblematischer :idea:

hier mein Auslesecode mit deiner Hilfe ;)

Code: Alles auswählen

import urllib2
import re
response = urllib2.urlopen("http://www.ntv.de/")
html = response.read()
p = re.compile('(?<=href=")(.*?)(?=")')
link_matches = p.findall(html)
print link_matches
Vielen Dank Gerold
doca82
User
Beiträge: 48
Registriert: Mittwoch 16. September 2009, 19:39
Wohnort: Berlin

aber warum gibt dieses Ding so diese Fehlermeldungen (hab es mal mit dem Protokoll probiert):

Code: Alles auswählen

import httplib
import re


def getLinks(url):
    conn = httplib.HTTPConnection(url)
    conn.request('GET' , '/')
    antw = conn.getresponse()
    html = antw.read()
    
    p = re.compile('(?<=href=")(.*?)(?=")')
    link_matches = p.findall(html)
    return link_matches

getLinks('http://www.touristiklinks.de')
Traceback (most recent call last):
File "blabla.py", line 15, in <module>
getLinks('http://www.touristiklinks.de')
File "blabla.py"", line 6, in getLinks
conn = httplib.HTTPConnection(url)
File "C:\Python26\lib\httplib.py", line 656, in __init__
self._set_hostport(host, port)
File "C:\Python26\lib\httplib.py", line 668, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: '//www.touristiklinks.de'
Benutzeravatar
Defnull
User
Beiträge: 778
Registriert: Donnerstag 18. Juni 2009, 22:09
Wohnort: Göttingen
Kontaktdaten:

Weil httplib die URL ohne das "http://" haben will.
Bottle: Micro Web Framework + Development Blog
Antworten