2011-01-26 1 views
1

구문 분석 방법을 잘 모르는 레이아웃이있는 HTML 텍스트 파일을 구문 분석하는 데 도움이 필요하며 실제로 도움을 사용할 수 있습니다. 지금까지Python 구문 분석 : 비표준 레이아웃의 html 테스트 파일에서 데이터를 가져옵니다.

코드 :

내가 할 노력하고있어
import urllib,os, urllib2, webbrowser, StringIO, re 
from BeautifulSoup import BeautifulSoup 
from urllib import urlopen 

urlfile = open('output.txt','r') 

html = urlfile 

soup = BeautifulSoup(''.join(html)) 

print soup.prettify() 
table = soup.find('table', id="dgProducts__ctl2_lblCountry") 
rows = table.findAll('<span id="dgProducts__ctl2_lblCountry">') 

for tr in rows: 
    cols = tr.findAll('td') 
for td in cols: 
    text = ''.join(td.find(text=True)) 
    print text+"|", 
print 

: 나는 .html 중에서 텍스트 파일에서 데이터를 추출하기 위해 찾고 있어요 를하고 다음과 같은 형식으로 표시해야 :

Header Row: Country Company Name Company Product Name  Status 
Data Row(s): 1  Ace   Desktop  Ace Vision Gold 

축약 된 .html 파일 데이터 구조 :

</tr><tr bgcolor="White"> 
    <td><font color="#330099" size="1"> 
     <span><font size="2"> 
      <input id="dgProducts__ctl12_ckCompare" type="checkbox" name="dgProducts:_ctl12:ckCompare" onclick="checkSelected(this.form, this);" /> 
      </font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblModel1"><font size="2"> 
      <a href='ProductDisplay.aspx?return=pm&action=view&search=true&productid=4592&ProductType=1&epeatcountryid=1'>Ace Vision 7HS</a></font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblCountry">United States</span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblProductCategory1"><font size="2">Desktops</font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblRating1"><font size="2">Gold</font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblPoints1">18</span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblEnergyStar">5.0</span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblMonitorType1"><font size="2"></font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblMonitorSize"><font size="2"></font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblListingDate1"><font size="2">3/16/2010</font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblStatus"><font size="2">Active</font></span> 
     </font></td><td><font color="#330099" size="1"> 
     <span id="dgProducts__ctl12_lblExceptions" align="center"><a href='#' onclick=ShowExceptions('Exceptions.aspx?id=4592');>  
      <img src='http://www.epeat.net/Images/inform.gif' title='Click to view exceptions' alt='Click to view exceptions' border='0'></a></span> 
     </font></td> 
+1

'html.read()'대신' ''.join (html)'을 발견했습니다. 자, 각자 자신에게 : –

+2

"지금까지의 코드"... "나는 무엇을하려고하는지"... 그리고? 작동하지 않는 것은 무엇입니까? 무슨 문제 있니? 어떤 도움이 필요합니까? –

답변

0

MiniDom 또는 xml.dom.minidom이라는 모듈을 사용하는 것이 좋습니다. XML과 HTML 파일을 쉽게 파싱 할 수 있습니다.