首页 > 下面一段html代码怎么用Python+正则一次性提取出来:标题,url,时间,简介组成一个字典?

下面一段html代码怎么用Python+正则一次性提取出来:标题,url,时间,简介组成一个字典?

                               <div class="list_item clearfix">
        <div class="item_top">
            <h2><a href="http://money.163.com/15/0902/09/B2GEL9V8002551G6.html">主营业务负增长 董明珠还自信?</a></h2>
                            <a href="http://money.163.com/15/0902/09/B2GEL9V8002551G6.html" title="主营业务负增长 董明珠还自信?" class="newsimg" lang="http://img1.cache.netease.com/stock/2015/9/2/2015090209124874ceb_550.jpg"><img src="http://s.cimg.163.com/stock/2015/9/2/2015090209124874ceb_550.jpg.119x83.jpg" alt="主营业务负增长 董明珠还自信?" /></a>                                <p>[摘要:8月30日晚间公布的格力电器上半年财报无疑给董明珠浇了一盆冷水。虽然董明珠嘴上仍在硬撑,但她的底气明显不足了。因为,格力的主营业务——空调出问题了。当格力开始不谈承诺的时候,董明珠最应该干的事,是静下心来好好反思,而不是把更多的心思花在无聊的口水战和吹牛逼 ...<br />
                <span class="time">2015-09-02 09:20:55</span>
            </p>
        </div>
        <div class="item_bottom">
            <ul class="mod_list">
                <li>董明珠应该静下心好好反思,而不是把心思花在口水战和吹牛逼上。</li>                                                            <li><a href="http://money.163.com/15/0902/09/B2GEL9V8002551G6.html">主营业务负增长 董明珠还自信?</a></li>                </ul>
        </div>
    </div>
                       <div class="list_item clearfix">
        <div class="item_top">
            <h2><a href="http://money.163.com/15/0831/11/B2BGVVQ0002551G6.html">中国手机的非洲历险记</a></h2>
                            <a href="http://money.163.com/15/0831/11/B2BGVVQ0002551G6.html" title="中国手机的非洲历险记" class="newsimg" lang="http://img4.cache.netease.com/stock/2015/8/31/201508311110060cc03.jpg"><img src="http://s.cimg.163.com/stock/2015/8/31/201508311110060cc03.jpg.119x83.jpg" alt="中国手机的非洲历险记" /></a>                                <p>[摘要:中国手机全球化版图扩张中,已经完成了两个阶段,国内市场红海一片,几乎没有太多眷恋;欧美发达国家,市场如堡垒般稳固,而且面临专利、渠道等麻烦,败走麦城似乎是命中注定的事儿。]原标题:[亦观察] No.616&nbsp;中国手机的非洲历险记如今,中国手机正悄然转移重点,把  ...<br />
                <span class="time">2015-08-31 11:25:30</span>
            </p>
        </div>

好像用Python的库直接解析HTML更简单


建议使用beautifulsoup库解析,和前端操作dom类似解析html


不需要使用正则,最好用BeautifulSoup解析HTML文档:

from bs4 import BeautifulSoup

html = '''
     <div class="list_item clearfix">
     .........你的html文档
     </div>
     '''
soup = BeautifulSoup(html)
tags = soup.find_all('div', class_='item_top')
news_list = []
for tag in tags:
    news_dict = {}
    news_dict['news_title'] = tag.h2.string
    news_dict['news_time'] = tag.span.string
    news_dict['news_abstract'] = tag.p.next_element
    news_dict['news_url'] = tag.h2.a.get('href')
    news_list.append(news_dict)

最终运行结果,每个新闻一个字典,所有字典在一个列表里:

print(news_list)

[
{
'news_abstract': '[摘要:8月30日晚间公布的格力电器上半年财报无疑给董明珠浇了一盆冷水。虽然董明珠嘴上仍在硬撑,但她的底气明显不足了。因为,格力的主营业务——空调出问题了。当格力开始不谈承诺的时候,董明珠最应该干的事,是静下心来好好反思,而不是把更多的心思花在无聊的口水战和吹牛逼 ...', 
'news_url': 'http://money.163.com/15/0902/09/B2GEL9V8002551G6.html', 
'news_time': '2015-09-02 09:20:55', 
'news_title': '主营业务负增长 董明珠还自信?'
}, 
{
'news_abstract': '[摘要:中国手机全球化版图扩张中,已经完成了两个阶段,国内市场红海一片,几乎没有太多眷恋;欧美发达国家,市场如堡垒般稳固,而且面临专利、渠道等麻烦,败走麦城似乎是命中注定的事儿。]原标题:[亦观察] No.616\xa0中国手机的非洲历险记如今,中国手机正悄然转移重点,把  ...',
'news_url': 'http://money.163.com/15/0831/11/B2BGVVQ0002551G6.html', 
'news_time': '2015-08-31 11:25:30', 
'news_title': '中国手机的非洲历险记'
}
]
【热门文章】
【热门文章】