html1="""
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>我的第一个网页</title>
<meta name="generator" content="EverEdit" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<div class="rows">
<a href="http://www.baidu.com/" target="_blank">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color1">
<span class="vfsd_a_title">百度</span>
</div>
</a>
<a href="http://www.google.com/" target="_blank">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color3">
<span class="vfsd_a_title">Google</span>
</div>
</a>
<a href="http://www.oschina.net/" target="_blank">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">
<span class="vfsd_a_title">Stack Overflow</span>
</div>
</a>
</div>
<p class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">你好
<span class="vfsd_a_title">CSDN</span>
</p>
<p class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">
<span class="vfsd_a_title">FaceBook</span>
</p>
<p class="nmn" id="nmn1">
<span class="vfsd_a_title">开源中国</span>
</p>
</body>
</html>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html1,'lxml')

print(soup.title)

####################输出:

<title>我的第一个网页</title>

print(soup.title.string)

####################输出:

我的第一个网页

print(soup.head)

####################输出:

<head>
<meta charset="utf-8"/>
<title>我的第一个网页</title>
<meta content="EverEdit" name="generator"/>
<meta content="" name="author"/>
<meta content="" name="keywords"/>
<meta content="" name="description"/>
</head>

for i,child in enumerate(soup.div.children):
  print(i,child)

####################输出:

['\n', <a href="http://www.baidu.com/" target="_blank">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color1">
<span class="vfsd_a_title">百度</span>
</div>
</a>, '\n', <a href="http://www.google.com/" target="_blank">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color3">
<span class="vfsd_a_title">Google</span>
</div>
</a>, '\n', <a href="http://www.oschina.net/" target="_blank">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">
<span class="vfsd_a_title">Stack Overflow</span>
</div>
</a>, '\n']

BeautifulSoup练习的更多相关文章

  1. Python爬虫小白入门(三)BeautifulSoup库

    # 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...

  2. 使用beautifulsoup与requests爬取数据

    1.安装需要的库 bs4 beautifulSoup  requests lxml如果使用mongodb存取数据,安装一下pymongo插件 2.常见问题 1> lxml安装问题 如果遇到lxm ...

  3. BeautifulSoup :功能使用

    # -*- coding: utf-8 -*- ''' # Author : Solomon Xie # Usage : 测试BeautifulSoup一些用法及容易出bug的地方 # Envirom ...

  4. BeautifulSoup研究一

    BeautifulSoup的文档见 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 其中.contents 会将换行也记录为一个子节 ...

  5. BeautifulSoup

    参考:http://www.freebuf.com/news/special/96763.html 相关资料:http://www.jb51.net/article/65287.htm 1.Pytho ...

  6. BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

    BeautifulSoup很赞的东西 最近出现一个问题:Python 3.3 soup=BeautifulSoup(urllib.request.urlopen(url_path),"htm ...

  7. beautifulSoup(1)

    import re from bs4 import BeautifulSoupdoc = ['<html><head><title>Page title</t ...

  8. python BeautifulSoup模块的简要介绍

    常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...

  9. BeautifulSoup 的用法

    转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python ...

  10. BeautifulSoup的选择器

    用BeautifulSoup查找指定标签(元素)的时候,有几种方法: soup=BeautifulSoup(html) 1.soup.find_all(tagName),返回一个指定Tag元素的列表 ...

随机推荐

  1. timer用作timestamp及其他

    niosii中使用时间戳是很有用的,可以查看代码的执行时间是多少,在使用timestamp的过程中遇到一些问题现在做一下记录. 1.硬件部分构建软核没什么,就加一个timer就行了,加完之后自动获得基 ...

  2. fn project 私有镜像发布

    1. 说明 fnproject 默认的docker registry 是 dockerhub 对于企业应用还是不太方便的 还好系统系统了配置参数方便我们进行配置,与开源harbor 进行集成 2. 使 ...

  3. php://input解决APP发送图片问题

    今天公司要求用APP发送一个图片到PHP程序接收并保存起来,而且中间还需要很多参数! 以前没有做过APP和PHP交互,这次算是一个挑战吧(对一个没有人指导实习生来说) 1.APP发1.jpg,而且带有 ...

  4. Spring读取properties资源文件

    我们知道可以通过读取资源文件流后加载到Properties对象,再使用该对象方法来获取资源文件.现在介绍下利用Spring内置对象来读取资源文件. 系统启动时加载资源文件链路:web.xml --&g ...

  5. 安装并配置工具以使用iOS进行构建

    Visual Studio 2015   Visual Studio文档的新家是docs.microsoft.com上的Visual Studio 2017文档 . 有关Visual Studio 2 ...

  6. HUSTOJ 昆虫繁殖(递推)

    昆虫繁殖 时间限制: 1 Sec  内存限制: 128 MB提交: 25  解决: 16[提交][状态][讨论版][命题人:quanxing] 题目描述 科学家在热带森林中发现了一种特殊的昆虫,这种昆 ...

  7. MySQL的Innodb缓存相关优化

    MySQL的Innodb缓存相关优化 INNODB 状态的部分解释 通过 命令 SHOW STATUS LIKE  'Innodb_buffer_pool_%' 查看  Innodb缓存使用率  (I ...

  8. thinkphp遇到的小问题,js文件中U方法不被解析

    我想在js文件中写ajax, 写完发现异常, 本以为是js文件中不支持ajax 后来发现时地址解析错误. 也就是U方法在js文件中不被解析. 貌似thinkphp解析,tpl文件中的一些元素. js文 ...

  9. MySQL router

    MySQL Router is a building block for high availability (HA) solutions. It simplifies application dev ...

  10. 第七章 JVM体系结构与工作方式

    JVM能跨计算机体系结构来执行Java字节码,主要是由于JVM屏蔽了与各个计算机平台的软件和硬件之间的差异. 7.1 JVM体系结构 7.1.1 何谓JVM 模拟一个计算机来达到一个计算机所具有的计算 ...