微博資料清洗(Python版)__Python

來源:互聯網
上載者:User

具體介紹請參考: 微博資料清洗(Java版)

本文是Python版本的介紹,只有資料清洗部分不含對Excel的操作,包含去除html標籤和去掉資訊裡的url地址。

Python的代碼相對java簡明了太多


# -*- coding: UTF-8 -*-'''Created on 2013年12月10日@author: daT dev.tao@gmail.com'''import redef dealHtmlTags(html):    '''    去掉html標籤    '''    from HTMLParser import HTMLParser    html=html.strip()    html=html.strip("\n")    result=[]    parse=HTMLParser()    parse.handle_data=result.append    parse.feed(html)    parse.close()    return "".join(result)def dealUrl(text):    '''    去掉微博資訊中的url地址    '''    return re.sub('''http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+''', '',text)        if __name__== "__main__":    html = """接下來一年,我希望在<SPAN style="COLOR: red">惠普</SPAN>電腦看到更人性化,各科技化的東西,更能提升視覺享受的東西。 地址:<A title=http://t.cn/8kUAX2z href="http://t.cn/8kUAX2z" target=_blank suda-data="key=tblog_search_v4.1&value=weibo_feed_url" :3651215114310513>http://t.cn/8kUAX2z<SPAN class=feedico_active></SPAN></A>    """    html =  dealHtmlTags(html)    print dealUrl(html)


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.