国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python采集百度百科的方法

2020-02-23 01:33:35
字體:
供稿:網(wǎng)友

本文實(shí)例講述了python采集百度百科的方法。分享給大家供大家參考。具體如下:

#!/usr/bin/python# -*- coding: utf-8 -*-#encoding=utf-8 #Filename:get_baike.pyimport urllib2,reimport sysdef getHtml(url,time=10): response = urllib2.urlopen(url,timeout=time) html = response.read() response.close() return htmldef 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 htmlif __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è)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永清县| 桓仁| 正蓝旗| 平利县| 尚志市| 白玉县| 勃利县| 汉中市| 镇江市| 新竹县| 徐州市| 呼玛县| 济源市| 海丰县| 临桂县| 株洲县| 洞头县| 宝兴县| 仙游县| 新巴尔虎右旗| 临西县| 平安县| 九寨沟县| 涟源市| 隆化县| 巴马| 莆田市| 平阴县| 荣昌县| 忻城县| 平乐县| 丹阳市| 沙田区| 婺源县| 浙江省| 游戏| 天门市| 香格里拉县| 宝应县| 股票| 长丰县|