• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python采集百度百科代码示范

python 水墨上仙 1396次浏览

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/994462.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采集百度百科代码示范
喜欢 (0)
加载中……