source form  http://www.bkjia.com/ASPjc/908009.html

昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客:

Python3 Win7安装 BeautifulSoup,按照里面简单的步骤就可以把BeautifulSoup装上啦,很简单的,表害怕

装好BeautifulSoup4之后,就让我们来好好享受这碗BeautifulSoup吧,哈哈

入门:

下面就来介绍一下BeautifulSoup吧,BeautifulSoup是一个可以从HTML或XML文件中提取数据的 Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间

在你知道有BeautifulSoup这货之前,如果你从一个文本中提取出自己想要的东西,那么估计你应该使用re模块,但先在如果给 你一个HTML文件让你提取出一些关键信息,如果再使用re模块,虽然也可以把信息提取出来,但是捏,你可能会绞尽脑汁苦思冥想查阅好多资料,现在,我们 有了BeautifulSoup,一切变得超级简单起来,对,就是这么简单

实践:

学习bs4最好的还是查看bs4的官方文档,有中文版的哦,猛点这里官方文档,看起来会很快,笔者花了大概一个下午的时间,把bs4的官方文档看了一遍,顺手也写了写里面的示例程序,如果你没多少时间的话,看看我下面的代码,估计你会很快上手的,相信我 (*^_^*)

__author__ = 'MrChen'

from bs4 import BeautifulSoup
#这是示例
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
"""
#初始化,实例化一个BeautifulSoup对象,参数可以是一个字符串,也可以是一个打开的文件比如open('mydoc.html')
soup = BeautifulSoup(html_doc) print(soup.title)
#输出:<title>The Dormouse's story</title> print(soup.title.parent)
#输出:<head><title>The Dormouse's story</title></head> print(soup.title.parent.parent)
#输出:
#<html><head><title>The Dormouse's story</title></head>
#<body>
#<p class="title"><b>The Dormouse's story</b></p>
#<p class="story">Once upon a time there were three little sisters; and their names were
#<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
#<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
#and they lived at the bottom of a well.</p>
#<p class="story">...</p>
#</body></html> print(soup.title.name)
#输出:title print(soup.title.parent.name)
#输出:head print(soup.title.parent.parent.name)
#输出:html print(soup.p)
#输出:<p class="title"><b>The Dormouse's story</b></p> print(soup.p['class'])
#输出:['title'] print(soup.a)
#输出:<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> print(soup.find_all('a'))
#输出:
#[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] print(soup.find(id = 'link3'))
#输出:<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> for link in soup.find_all('a'):
print(link.get('href'))
#输出:
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie print(soup.getText())
#输出:
# The Dormouse's story
#
# The Dormouse's story
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
# ... print('all tags : <<<<<<')
for tag in soup.find_all(True):
print(tag.name)
#输出:
#html
#head
#title
#body
#p
#b
#p
#a
#a
#a
#p

怎使用python+beautifulsoup获取img中alt的中文信息

你好:
请看下面代码:
from bs4 import
BeautifulSouphtml="你的地址"soup=
BeautifulSoup(html)trs=soup.findAll("img")length=len(trs)for i in
range(length): print trs[i].attrs["alt"]记得采纳哦!

python使用BeautifulSoup解析html出现的问题

用这两个参数:findAll('div',{'class':'content'})

python BeautifulSoup4的更多相关文章

  1. Python BeautifulSoup4 使用指南

    前言: 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Python3 Win7安装 BeautifulSoup,依照里面简单的步骤就能够把Beautifu ...

  2. python - beautifulsoup4模块

    # beautifulsoup4学习 # 是一个python模块 用于接受一个HTML 或 XML 字符串,然后将其进行格式化,之后便可以使用模块提供的方法进行快速查找指定元素, # 从而是的在HTM ...

  3. 【python+beautifulsoup4】Beautifulsoup4

    Beautiful soup将复杂HTML文档转换成一个复杂的属性结构,每个节点都是python对象,所有对象可归纳为4种Tag,NavigableString,BeautifulSoup,Comme ...

  4. 【python+beautifulsoup4】Python中安装bs4后,pycharm报错ModuleNotFoundError: No module named 'bs4'

    本文主要分享关于在对应python版本中安装beautifulsoup之后,在代码执行时还会提示“No module named 'bs4'”的问题. 安装beautifsoup4 在命令窗口执行 p ...

  5. Python BeautifulSoup4 爬虫基础、多线程学习

    针对 崔庆才老师 的 https://ssr1.scrape.center 的爬虫基础练习.Threading多线程库.Time库.json库.BeautifulSoup4 爬虫库.py基本语法

  6. python BeautifulSoup4 获取 script 节点问题

    在爬取12306站点名时发现,BeautifulSoup检索不到station_version的节点 因为script标签在</html>之外,如果用‘lxml’解析器会忽略这一部分,而使 ...

  7. python BeautifulSoup4解析网页

    html = """ <html><head><title>The Dormouse's story</title>< ...

  8. python学习目录(转载)

    python基础篇 python 基础知识    python 初始python    python 字符编码    python 类型及变量    python 字符串详解 python 列表详解 ...

  9. Yeelink初步体验

    环境 Qemu: 2.8.0 开发板:vexpress-ca9   概述     前面的博文已经使我们的虚拟开发板具备了访问外网的目的,离物联网越来越近了.要玩物联网,Yeelink不得不说,它提供了 ...

随机推荐

  1. 与TCP/IP协议的初次见面(一)

    引言 最近LZ有了一点时间,于是便拿出TCP/IP的书本开始啃.开始的时候,啃起来枯燥无味,现在好不容易有点开窍,于是赶忙记录一下,生怕自己一转眼就给忘了.不过计算机系统原理就有点可惜了,最近一直没时 ...

  2. JavaScript学习笔记- 正则表达式常用字符集及方法

    正则表达式修饰符(修饰符 可以在全局搜索中不区分大小写) i(ignoreCase)执行对大小写不敏感的匹配 g (global)     执行全局匹配(查找所有匹配而非在找到第一个匹配后停止) m( ...

  3. 发短信的简单实现——C#版

    为了验证操作人的身份,界面中通常会有获取验证码的功能.及点击获取验证码就会往你输入的手机号里面发送一条短信进行验证. 最近公司给我的任务中也包含这个功能,那么接下来就让我讲解下. ---------- ...

  4. JS iframe元素和父页面元素互访

    说明:以下内容来自互联网 [1]子页面取得父页面的dom对象   parent.window.$('#id').val("");   [2]父页面取得子页面的对象   $(wind ...

  5. java 上传图片 并压缩图片大小

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  6. ScrollView内部元素如何做到fill_parent 或者 match_parent?

    转  : http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0704/1629.html ScrollView滚动视图是指当拥有很多 ...

  7. Shell脚本_判断根分区使用率

    vim gen_fen_qu_shi_yong_lv.sh chmod  755 gen_fen_qu_shi_yong_lv.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. python之旅3

    1 collections系列 方法如下 class Counter(dict): '''Dict subclass for counting hashable items. Sometimes ca ...

  9. ps制作gif图片

    本文自学内容来自这里 PS版本是CS6: 制作效果 步骤 1.下载素材 2.打开ps,添加素材 文件->打开->选择所有需要的素材全部打开(如图,已将需要的3个素材全部打开) 3.将素材放 ...

  10. js-延迟处理函数

    <script type="text/javascript"> var i = setTimeout('check()',5000); function check() ...