2016-11-29 5 views
0

import urllib von BS4 import BeautifulSoup import reFehler beim urllib in Python 2.7

SUMT = 0

html = urllib.urlopen ('http://python-data.dr-chuck.net/comments_338391.html') .Lesen()

Suppe = BeautifulSoup (html)

tags = Suppe ('Spanne')

für lne in tags: LNE = Str (LNE) data = re.findall ('[0-9] +', LNE) data [0] = int (data [0]) = SUMT SUMT + data [0]

Druck SUMT

Fehler:

IOError: [Errno socket error] [Errno 11004] getaddrinfo failed 
+0

Mögliches Duplikat von ["getaddrinfo fehlgeschlagen", was bedeutet das?] (Http://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean) – Tzach

Antwort

1

Bitte beachten Sie, dass urllib.urlopen ist veraltet; Sie sollten urllib2.urlopen verwenden.

Wie auch immer, für mich funktionieren beide Versionen gut.

import urllib2 
import re 

if __name__ == '__main__': 
    url = 'http://python-data.dr-chuck.net/comments_338391.html' 
    comments = {} 
    pattern = re.compile('<tr><td>(?P<name>.+?)</td>.+?class="comments">(?P<count>\d+)</span>.+?') 
    for line in urllib2.urlopen(url).read().split('\n'): 
     m = pattern.match(line) 
     if m: 
      comments[m.group('name')] = int(m.group('count')) 
    print(comments) 

Ausbeuten:

{'Caidan': 28, 'Haylie': 59, 'Fikret': 43, 'Tabbitha': 54, 'Rybecca': 70, 'Pearl': 45, 'Kiri': 72, 'Storm': 66, 'Kelum': 55, 'Elisau': 30, 'Lexi': 70, 'Cobain': 2, 'Theodore': 36, 'Ammer': 26, 'Carris': 87, 'Fion': 10, 'Derick': 28, 'Shalamar': 98, 'Adil': 93, 'Wasif': 54, 'Yasin': 78, 'Mhyren': 92, 'Kodi': 75, 'Nikela': 98, 'Lorena': 76, 'Seth': 68, 'Lillia': 91, 'Nitya': 26, 'Tigan': 73, 'Jaii': 11, 'Kamran': 74, 'Arianna': 12, 'Mercedes': 92, 'Gregory': 40, 'Umaima': 83, 'Rhylee': 26, 'Kaia': 91, 'Hamid': 33, 'Lucien': 5, 'Zacharias': 92, 'Abir': 35, 'Teejay': 51, 'Muir': 43, 'Hena': 84, 'Alanas': 16, 'Lybi': 91, 'Atiya': 87, 'Kayleb': 7, 'Fletcher': 87, 'Lisandro': 78}

d.h .: funktioniert für mich.