2013-04-16 2 views
0
import os, re, sys, urllib2 
from bs4 import BeautifulSoup 
import lxml 

html = urllib2.urlopen("http://www.hoerzu.de/tv-programm/jetzt/") 
soup = BeautifulSoup(html, "lxml") 
divs = soup.find_all("div", {"class":"block"}) 
print len(divs) 

출력 : 나는 또한 시도BeautifulSoup/lxml : 큰 요소에 문제가 있습니까?

ActivePython 2.7.2.5 (ActiveState Software Inc.) based on 
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import os, re, sys, urllib2 
>>> from bs4 import BeautifulSoup 
>>> import lxml 
>>> 
>>> html = urllib2.urlopen("http://www.hoerzu.de/tv-programm/jetzt/") 
>>> soup = BeautifulSoup(html, "lxml") 
>>> divs = soup.find_all("div", {"class":"block"}) 
>>> print len(divs) 
2 

: 같은 결과

divs = soup.find_all(class_="block") 

...

그러나이 조건에 맞는 11 개 요소가 있습니다. 그래서 최대 요소 크기 resp 같은 모든 제한이 있습니다. 어떻게 모든 요소를 ​​얻을 수 있습니까? 그것은 나를 위해 1 인쇄 (lxml 사용) 원래의 코드와

import os, re, sys, urllib2 
from bs4 import BeautifulSoup 
import lxml 

html = urllib2.urlopen("http://www.hoerzu.de/tv-programm/jetzt/") 
soup = BeautifulSoup(html, "html.parser") 
divs = soup.find_all("div", {"class":"block"}) 
print len(divs) 

, 그러나 이것은 11을 인쇄 :

+1

나는 침대에 가고 싶다고 생각합니다. –

+0

코드에 11 개의 div가 있습니다. – JosefAssad

+0

@JosefAssad와 동일합니다. 첫 코드로 11을 얻고 있습니다. – ton1c

답변

4

가장 쉬운 방법은 아마도 'html.parser'대신 'LXML'를 사용하고 있습니다. lxml은 관대하지만 관대하지는 않습니다.이 페이지는 html.parser입니다. 당신이 tidy를 통해 실행하는 경우

페이지가000 한 이상 경고를 가지고 있습니다. 유효하지 않은 문자 코드 (예 : <div>)를 포함한 문자는 </과 같은 문자는 허용되지 않습니다.

+0

plains lxml이 모든 div를 얻었 기 때문에 beautifulsoup의 문제로 보입니다. 그래서 나는 최근에 일반 lxml로 전환했습니다. – lorus