-3

我有以下格式的html文件。我想用python解析它。但是,我对使用 xml 模块一无所知。非常欢迎您的建议。

注意:再次对不起我的无知。问题不具​​体。但是,由于我对这样的解析脚本感到沮丧,我确实想得到一个由答案人(谢谢大家)描述的具体答案作为起点。希望你能理解。

<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>

可能重复:
使用 Python 从 HTML 文件中提取文本

4

2 回答 2

3

我建议你看看 Python 库BeautifulSoup。它可以帮助您导航和搜索 HTML 数据。

于 2012-05-02T07:56:37.600 回答
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 ')
于 2012-05-02T21:39:33.103 回答