2013-06-27 3 views
1

나는 형식 다음 테이블에 항목을 읽으려면 :구문 분석 테이블

<table cellspacing="0" cellpadding="4"> 

stuff 

</table> 

내 현재의 방법으로 이것을 사용하고 있습니다 :

pg = urllib2.urlopen(req).read() 
page = BeautifulSoup(pg) 
table = page.find('table', cellpadding = 4, cellspacing = 0) 

table 수 '를 태그를 올바르게 읽지 마십시오.이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

+1

코드가 저에게 맞습니다 – TerryA

답변

1

나는 당신의 코드가 BS4와 함께 작동 BeautifulSoup로 버전 3, 4 모두이 테스트를했습니다, 그래서 당신은이 정상적으로 BS3을 계속하려는 경우, 3

>>> from bs4 import BeautifulSoup as BS4 # Version 4 
>>> from BeautifulSoup import BeautifulSoup as BS3 # Version 3 
>>> bs3soup = BS3("""<table cellspacing="0" cellpadding="4"> 
... 
... stuff 
... 
... </table>""") 
>>> bs4soup = BS4("""<table cellspacing="0" cellpadding="4"> 
... 
... stuff 
... 
... </table>""") 
>>> bs3soup.find('table', cellpadding = 4, cellspacing = 0) # None 
>>> bs4soup.find('table', cellpadding = 4, cellspacing = 0) 
<table cellpadding="4" cellspacing="0"> 

stuff 

</table> 

그래서 버전을 사용하고 있어야합니다 수정 :

>>> soup.find('table', cellpaddin="4", cellspacing="0") # Notice how the integers are now strings, like in the HTML. 

그러나 버전 4 (from bs4 import BeautifulSoup)를 사용해야합니다.

+0

그래, 효과가있다. –