python采集百度百科的方法
更新時間:2015年06月05日 10:04:58 作者:兩把刷子
這篇文章主要介紹了python采集百度百科的方法,涉及Python正則匹配及頁面抓取的相關技巧,需要的朋友可以參考下
本文實例講述了python采集百度百科的方法。分享給大家供大家參考。具體如下:
#!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:get_baike.py import urllib2,re import sys def getHtml(url,time=10): response = urllib2.urlopen(url,timeout=time) html = response.read() response.close() return html def clearBlank(html): if len(html) == 0 : return '' html = re.sub('\r|\n|\t','',html) while html.find(" ")!=-1 or html.find(' ')!=-1 : html = html.replace(' ',' ').replace(' ',' ') return html if __name__ == '__main__': html = getHtml('http://baike.baidu.com/view/4617031.htm',10) html = html.decode('gb2312','replace').encode('utf-8') #轉碼 title_reg = r'<h1 class="title" id="[\d]+">(.*?)</h1>' content_reg = r'<div class="card-summary-content">(.*?)</p>' title = re.compile(title_reg).findall(html) content = re.compile(content_reg).findall(html) title[0] = re.sub(r'<[^>]*?>', '', title[0]) content[0] = re.sub(r'<[^>]*?>', '', content[0]) print title[0] print '#######################' print content[0]
希望本文所述對大家的Python程序設計有所幫助。
相關文章
Python內置數(shù)據(jù)類型list各方法的性能測試過程解析
這篇文章主要介紹了Python內置數(shù)據(jù)類型list各方法的性能測試過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01pyqt5 textEdit、lineEdit操作的示例代碼
這篇文章主要介紹了pyqt5 textEdit、lineEdit操作的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Python實現(xiàn)求解最大公約數(shù)的五種方法總結
求最大公約數(shù)是習題中比較常見的類型,本文小編將給大家提供五種比較常見的算法,都是用Python語言實現(xiàn)的,感興趣的小伙伴可以了解一下2022-07-07Pandas中DataFrame基本函數(shù)整理(小結)
這篇文章主要介紹了Pandas中DataFrame基本函數(shù)整理(小結),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07