2012-05-02 1 views
-3

다음 형식의 html 파일이 있습니다. 파이썬을 사용하여 구문 분석하고 싶습니다. 그러나 XML 모듈을 사용하는 것에 대해서는 무지합니다. 귀하의 제안은 대단히 환영합니다.Python을 사용하여 HTML 파일 구문 분석 : 시작 지점

참고 : 다시 알지 못해 죄송합니다. 질문은 구체적이지 않습니다. 그러나 필자는 그러한 구문 분석 스크립트에 좌절감을 느꼈으므로 답변 담당자 (모두에게 감사드립니다)가 시작 지점으로 설명하는 구체적인 대답을 얻고 싶습니다. 당신이 이해할 수 있기를 바랍니다.

<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Weibo Landscape: Historical Archive of 800 Verified Accounts</title> 
    </head> 
    <body> 
<div><br> 
related 1-th-weibo:<br> 
mid:3365546399651413<br> 
score:-5.76427445942 <br> 
uid:1893278624 <br> 
link:<a href="http://weibo.com/1893278624/xrv9ZEuLX" target="_blank">source</a> <br> 
time:Thu Oct 06 17:10:59 +0800 2011 <br> 
content: Zuccotti Park。 <br> 
<br></div> 
<div><br> 
related 2-th-weibo:<br> 
mid:3366839418074456<br> 
score:-5.80535767804 <br> 
uid:1813080181 <br> 
link:<a href="http://weibo.com/1813080181/xs2NvxSxa" target="_blank">source</a> <br> 
time:Mon Oct 10 06:48:53 +0800 2011 <br> 
content:rt the tweet <br> 
rtMid:3366833975690765 <br> 
rtUid:1893801487 <br> 
rtContent:#ows#here is the content and the link http://t.cn/aFLBgr <br> 
<br></div> 

    </body> 
    </html> 

가능한 중복 :
Extracting text from HTML file using Python

+1

그래서 Python으로 HTML을 구문 분석하는 것에 관해 많은 질문이 있습니다. 주위를 검색하는 데 몇 분을 보내십시오. 위에 링크 된 질문에서'HTMLParser' 예제를 보시려면 –

+0

을 클릭하십시오. 나는 내가 원하는 것을 찾지 못했습니다. 결과를 텍스트로 변환하는 것보다 결과를 더 구조화하기를 원합니다. –

+1

이것은 단지 하나의 예입니다. 몇 가지 질문이 있고 HTML 분석에 대해 : http://stackoverflow.com/search?q=python%20html%20parse –

답변

1

나는 운동으로 이것을했다. 이것이 여전히 유용하다면 올바른 방향으로 나아갈 것입니다.

# -*- coding: utf-8 -*- 

from BeautifulSoup import BeautifulSoup 


html = '''<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Weibo Landscape: Historical Archive of 800 Verified Accounts</title> 
    </head> 
    <body> 
<div><br> 
related 1-th-weibo:<br> 
mid:3365546399651413<br> 
score:-5.76427445942 <br> 
uid:1893278624 <br> 
link:<a href="http://weibo.com/1893278624/xrv9ZEuLX" target="_blank">source</a> <br> 
time:Thu Oct 06 17:10:59 +0800 2011 <br> 
content: Zuccotti Park。 <br> 
<br></div> 
<div><br> 
related 2-th-weibo:<br> 
mid:3366839418074456<br> 
score:-5.80535767804 <br> 
uid:1813080181 <br> 
link:<a href="http://weibo.com/1813080181/xs2NvxSxa" target="_blank">source</a> <br> 
time:Mon Oct 10 06:48:53 +0800 2011 <br> 
content:rt the tweet <br> 
rtMid:3366833975690765 <br> 
rtUid:1893801487 <br> 
rtContent:#ows#here is the content and the link http://t.cn/aFLBgr <br> 
<br></div> 

    </body> 
    </html>''' 

data = [] 
soup = BeautifulSoup(html) 
divs = soup.findAll('div') 
for div in divs: 
    div_string = str(div) 
    div_string = div_string.replace('<br />', '') 
    div_list = div_string.split('\n') 
    div_list = div_list[1:-1] 
    record = [] 
    for item in div_list: 
     record.append(tuple(item.split(':', 1))) 
    data.append(record) 

for record in data: 
    for field in record: 
     print field 
    print '--------------' 

샘플 데이터를 사용하면이 결과가 표시됩니다. 추가 처리는 원하는 구조로 쉽게 마사지 할 수 있어야합니다.

('related 1-th-weibo', '') 
('mid', '3365546399651413') 
('score', '-5.76427445942 ') 
('uid', '1893278624 ') 
('link', '<a href="http://weibo.com/1893278624/xrv9ZEuLX" target="_blank">source</a> ') 
('time', 'Thu Oct 06 17:10:59 +0800 2011 ') 
('content', ' Zuccotti Park\xe3\x80\x82 ') 
-------------- 
('related 2-th-weibo', '') 
('mid', '3366839418074456') 
('score', '-5.80535767804 ') 
('uid', '1813080181 ') 
('link', '<a href="http://weibo.com/1813080181/xs2NvxSxa" target="_blank">source</a> ') 
('time', 'Mon Oct 10 06:48:53 +0800 2011 ') 
('content', 'rt the tweet ') 
('rtMid', '3366833975690765 ') 
('rtUid', '1893801487 ') 
('rtContent', '#ows#here is the content and the link http://t.cn/aFLBgr ') 
+0

좋은 답변입니다. 고마워. –

3

난 당신이 파이썬 라이브러리 BeautifulSoup를 살펴 것이 좋습니다. HTML 데이터 탐색 및 검색에 도움이됩니다.