学习自http://rsj217.diandian.com/post/2012-11-01/40041235132

#/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib2
import sys
import refrom bs4 import BeautifulSoup           # HTML
from bs4 import BeautifulStoneSoup      # XML
import bs4                              # ALLdoc = ['<html><head><title>Page title</title></head>','<body><p id="firstpara" align="center">This is paragraph <b>one</b>.','<p id="secondpara" align="blah">This is paragraph <b>two</b>.','</html>']
# BeautifulSoup 接受一个字符串参数
soup = BeautifulSoup(''.join(doc))
print type(soup)
print type(soup.html)
print type(soup.title.string)#BeautifulSoup文档树有三种基本对象
print
#BeautifulSoup对象
html = soup.html
print type(html)
print htmlprint
#BeautifulSoup.Tag
title = soup.title
print type(title)
print titleprint
#BeautifulSoup.NavigableString
contents = soup.contents
print type(contents)
print contentsprint
#使用contents方法查看文档树层级结构
print len(soup.contents[0].contents)
print soup.contents[0].contents[0]
print soup.contents[0].contents[1]
print len(soup.contents[0].contents[0])
print soup.contents[0].contents[0].contents[0]
print soup.contents[0].contents[0].contents[0].contents[0]#获取树的子代元素,类似深度遍历
print
head = html.next
print type(head)
print headprint
title = head.next
print type(title)
print titleprint
title_content = title.next
print type(title_content)
print title_contentprint
body = title_content.next
print type(body)
print body#使用replacewith方法替换对象
print
print head
print head.parent
head.replaceWith('head was replace')
print head.parent #输出空,因为原数据保留并被剪除
print head #没有改变正常输出
print soup.head #输出空,head对象已不存在
print soup #文档对象已经被修改print
#使用find,findAll方法进行搜索
print soup.findAll('p')
print
print soup.findAll('p',id='firstpara')
print
#传一个属性或多个属性对
print soup.findAll('p',{'align':'blah'})
#使用正则表达式
print soup.findAll(id=re.compile("para$"))#读取和修改属性
print
p1 = soup.p
print p1
print p1['id']
p1['id'] = 'changeid'
print p1 #已被修改
print soup #文档对象已经被修改

转载于:https://www.cnblogs.com/suncoolcat/p/3328937.html

BeautifulSoup学习笔记相关推荐

  1. python︱HTML网页解析BeautifulSoup学习笔记

    一.载入html页面信息 一种是网站在线的网页.一种是下载下来的静态网页. 1.在线网页 参考<python用BeautifulSoup库简单爬虫入门+案例(爬取妹子图)>中的载入内容: ...

  2. python分组求和_Python学习笔记之pandas索引列、过滤、分组、求和功能示例

    本文实例讲述了Python学习笔记之pandas索引列.过滤.分组.求和功能.分享给大家供大家参考,具体如下: 前面我们已经把519961(基金编码)这种基金的历史净值明细表html内容抓取到了本地, ...

  3. Python3 爬虫学习笔记 C10【数据储存系列 — MySQL】

    Python3 爬虫学习笔记第十章 -- [数据储存系列 - MySQL] 文章目录 [10.1]MySQL 基本操作语句 数据库操作 表操作 表的结构 表的数据 [10.2]Python 连接 My ...

  4. python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑

    python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件? 当我们点开下载页时, 一 ...

  5. python 网页爬虫作业调度_第3次作业-MOOC学习笔记:Python网络爬虫与信息提取

    1.注册中国大学MOOC 2.选择北京理工大学嵩天老师的<Python网络爬虫与信息提取>MOOC课程 3.学习完成第0周至第4周的课程内容,并完成各周作业. 4.提供图片或网站显示的学习 ...

  6. 英语学习笔记2019-10-25

    英语学习笔记2019-10-25 We should try to know more about the dynamics of the computer industry so that we c ...

  7. Python学习笔记:爬取网页图片

    Python学习笔记:爬取网页图片 上次我们利用requests与BeautifulSoup爬取了豆瓣<下町火箭>短评,这次我们来学习爬取网页图片. 比如想爬取下面这张网页的所有图片.网址 ...

  8. 关于《Python绝技:运用Python成为顶级黑客》的学习笔记

    本篇文章主要把<Python绝技:运用Python成为顶级黑客>中的代码敲一遍,学学Python安全相关的编程与思路,然后根据具体的情况修改一下代码. 第一章--入门 1.准备开发环境 安 ...

  9. python爬取b站视频封面_学习笔记(4)[Python爬虫]:爬取B站搜索界面的所有视频的封面...

    学习笔记(4)[Python爬虫]:爬取B站搜索界面的所有视频的封面 import os import requests import re import json from bs4 import B ...

最新文章

  1. web前端(13)—— 了解JavaScript,JavaScript的引入方式
  2. 《你不可不知的50个建筑学知识》之哥特式建筑
  3. 防静电塑料包装袋周围的静电场
  4. selenium V1.0和V2.0差别对比
  5. ccf-csp #201812-2 小明放学 (100分 附解析)
  6. C语言--在终端输入多行信息,找出包含“ould”的行,并打印改行
  7. adb无法连接安卓手机
  8. js 匿名函数_javascript:函数的使用
  9. 虚拟机ping不通宿主机,宿主机能ping通虚拟机
  10. cent os7 安装elasticsearch-7.9.3(伪集群)和kibana-7.9.3
  11. 如何获取Android应用签名?
  12. 开源Android自定义思维导图控件ThinkMap树状图TreeView(类似xMind那种效果)
  13. LambdaQueryWrapper的不同写法
  14. 机器学习岗位面试总结:简历应该关注的5个重点
  15. 关于小米历史官方刷机包
  16. 【计算机网络】第六部分 应用层(25) 域名空间
  17. java 雪崩效应,七、微服务架构中的“雪崩效应”
  18. springCloud微服务生态圈囊括—— 服务注册,服务调用,服务降级,熔断。(1)
  19. untiy TextMeshPro(简称TMP)组件详细内容
  20. mongdb 下载慢,解决

热门文章

  1. sap运维要做哪些工作_社会工作师可以做哪些工作?考试难不难?
  2. ios 获取沙盒文件名_iOS怎么获取并移除沙盒中Documents文件夹下的所有文件
  3. 如何知道现在是否单用户模式_新手运营Shopee现在是否来得及,商品的转化如何提高?...
  4. 哪些模块可用于python性能分析_Python调用C模块以及性能分析
  5. matlab中读文件的行数_[转载]MATLAB中获取大型文本文件行数方法研究(转)
  6. php转移动,php移动或拷贝文件夹
  7. 编程指南_今晚7点,译者编程入门指南抽奖!
  8. zookeeper3.4集群搭建
  9. Linux应用层的定时器Timer使用详解【转】
  10. modify timezone