python采集百度百科的方法
更新時間:2015年06月05日 10:04:58 作者:兩把刷子
這篇文章主要介紹了python采集百度百科的方法,涉及Python正則匹配及頁面抓取的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了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') #轉(zhuǎn)碼 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程序設(shè)計有所幫助。
您可能感興趣的文章:
- Python爬蟲:url中帶字典列表參數(shù)的編碼轉(zhuǎn)換方法
- 解決安裝python3.7.4報錯Can''''t connect to HTTPS URL because the SSL module is not available
- Python3模擬curl發(fā)送post請求操作示例
- python re正則匹配網(wǎng)頁中圖片url地址的方法
- Python2和Python3中urllib庫中urlencode的使用注意事項
- python爬蟲之urllib3的使用示例
- python 重定向獲取真實url的方法
- python采集百度搜索結(jié)果帶有特定URL的鏈接代碼實例
相關(guān)文章
零基礎(chǔ)也能看懂的python內(nèi)置csv模塊教程
這篇博客就為你介紹一個知識點,python 內(nèi)置模塊 csv 。讓大家一文就看懂csv csv(Comma-Separated Values)文件是什么?以及python 中的 csv 文件清晰解法讀取文件寫入文件 csv 文件其它說明2021-11-11Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析
這篇文章主要介紹了Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-01-01pyqt5 textEdit、lineEdit操作的示例代碼
這篇文章主要介紹了pyqt5 textEdit、lineEdit操作的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-08-08Python實現(xiàn)求解最大公約數(shù)的五種方法總結(jié)
求最大公約數(shù)是習題中比較常見的類型,本文小編將給大家提供五種比較常見的算法,都是用Python語言實現(xiàn)的,感興趣的小伙伴可以了解一下2022-07-07Pandas中DataFrame基本函數(shù)整理(小結(jié))
這篇文章主要介紹了Pandas中DataFrame基本函數(shù)整理(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-07-07