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') #轉(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程序設計有所幫助。
您可能感興趣的文章:
- 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的鏈接代碼實例
相關文章
Python使用itchat模塊實現(xiàn)簡單的微信控制電腦功能示例
這篇文章主要介紹了Python使用itchat模塊實現(xiàn)簡單的微信控制電腦功能,結(jié)合實例形式分析了Python基于itchat模塊控制電腦實現(xiàn)運行程序、截圖等相關操作技巧,需要的朋友可以參考下2019-08-08
python實現(xiàn)TCP服務器端與客戶端的方法詳解
這篇文章主要介紹了python實現(xiàn)TCP服務器端與客戶端的方法,以實例形式詳解分析了Python實現(xiàn)服務器端與客戶端的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
Biblibili視頻投稿接口分析并以Python實現(xiàn)自動投稿功能
這篇文章主要介紹了Biblibili視頻投稿接口分析并以Python實現(xiàn)自動投稿功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
虛擬環(huán)境及venv和virtualenv的區(qū)別說明
這篇文章主要介紹了虛擬環(huán)境及venv和virtualenv的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

