python采集百度百科的方法

作者:两把刷子 时间:2023-01-12 03:48:35 

本文实例讲述了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,采集
0
投稿

猜你喜欢

  • JDBC连接MySQL数据库关键的四个步骤

    2009-12-17 12:06:00
  • sqlserver2005 行列转换实现方法

    2024-01-14 01:34:56
  • 与 Function 和 Object 相关的有趣代码

    2010-05-07 12:29:00
  • uniapp小程序之配置首页搜索框功能的实现

    2024-04-22 13:03:28
  • VSCode下配置python调试运行环境的方法

    2023-03-27 10:15:40
  • 内容适应形式

    2010-03-18 16:09:00
  • 使用webpack-dev-server处理跨域请求的方法

    2024-04-29 13:44:19
  • python用pip install时安装失败的一系列问题及解决方法

    2021-10-15 23:11:43
  • Python批量修改文本文件内容的方法

    2022-07-16 08:37:43
  • 热门问题python爬虫的效率如何提高

    2023-06-11 03:21:40
  • python中@property的作用和getter setter的解释

    2022-08-14 21:12:39
  • python 列表删除所有指定元素的方法

    2021-11-21 22:55:20
  • python3+opencv 使用灰度直方图来判断图片的亮暗操作

    2024-01-01 07:07:23
  • python计算Content-MD5并获取文件的Content-MD5值方式

    2022-09-27 00:53:29
  • Golang项目在github创建release后自动生成二进制文件的方法

    2024-05-22 10:17:50
  • Mysql字符串截取及获取指定字符串中的数据

    2024-01-24 21:07:46
  • Python通过Manager方式实现多个无关联进程共享数据的实现

    2021-12-27 04:03:17
  • 关于Python可视化Dash工具之plotly基本图形示例详解

    2023-08-13 15:51:57
  • Python实现的井字棋(Tic Tac Toe)游戏示例

    2023-03-08 23:22:34
  • Python生成器generator原理及用法解析

    2021-10-14 14:00:13
  • asp之家 网络编程 m.aspxhome.com