2013-05-24 5 views
0

나는 원하는 데이터를 얻는데 어려움을 겪고 있으며 BS 사용법을 알고 있다면 매우 간단하다고 확신합니다. 나는이 문서들을 읽은 후에 쓸데없이 몇 시간 동안이 권리를 얻으려고 애썼다.아름다운 스프 HTML 추출

현재 내 코드이 파이썬에 출력합니다

[<td>0.32%</td>, <td><span class="neg color ">&gt;-0.01</span></td>, <td>0.29%</td>, <td>0.38%</td>, <td><span class="neu">0.00</span></td>] 

어떻게 그냥 태그를 포함하지 않는 TD 태그의 내용을 분리 것인가?

즉 0.32 %, 0.29 %, 0.38 % 만보고 싶습니다.

감사합니다.

import urllib2 
from bs4 import BeautifulSoup 

fturl = 'http://markets.ft.com/research/Markets/Bonds' 
ftcontent = urllib2.urlopen(fturl).read() 
soup = BeautifulSoup(ftcontent) 

ftdata = soup.find(name="div", attrs={'class':'wsodModuleContent'}).find_all(name="td",  attrs={'class':''}) 
+0

PLZ이 우리를 보여줍니다. – pypat

답변

2

당신이 좋아 솔루션입니다 :

html_txt = """<td>0.32%</td>, <td><span class="neg color"> 
    &gt;-0.01</span></td>, <td>0.29%</td>, <td>0.38%</td>, 
    <td><span class="neu">0.00</span></td> 
    """ 
from bs4 import BeautifulSoup 
soup = BeautifulSoup(html_txt) 
print [tag.text for tag in soup.find_all('td') if tag.text.strip().endswith("%")] 

출력은 다음과 같습니다 당신이 지금까지 시도했다

[u'0.32%', u'0.29%', u'0.38%'] 
+0

나를 위해 일한다, 고마워. – Reno

관련 문제