python BeautifulSoup4
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的更多相关文章
- Python BeautifulSoup4 使用指南
前言: 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Python3 Win7安装 BeautifulSoup,依照里面简单的步骤就能够把Beautifu ...
- python - beautifulsoup4模块
# beautifulsoup4学习 # 是一个python模块 用于接受一个HTML 或 XML 字符串,然后将其进行格式化,之后便可以使用模块提供的方法进行快速查找指定元素, # 从而是的在HTM ...
- 【python+beautifulsoup4】Beautifulsoup4
Beautiful soup将复杂HTML文档转换成一个复杂的属性结构,每个节点都是python对象,所有对象可归纳为4种Tag,NavigableString,BeautifulSoup,Comme ...
- 【python+beautifulsoup4】Python中安装bs4后,pycharm报错ModuleNotFoundError: No module named 'bs4'
本文主要分享关于在对应python版本中安装beautifulsoup之后,在代码执行时还会提示“No module named 'bs4'”的问题. 安装beautifsoup4 在命令窗口执行 p ...
- Python BeautifulSoup4 爬虫基础、多线程学习
针对 崔庆才老师 的 https://ssr1.scrape.center 的爬虫基础练习.Threading多线程库.Time库.json库.BeautifulSoup4 爬虫库.py基本语法
- python BeautifulSoup4 获取 script 节点问题
在爬取12306站点名时发现,BeautifulSoup检索不到station_version的节点 因为script标签在</html>之外,如果用‘lxml’解析器会忽略这一部分,而使 ...
- python BeautifulSoup4解析网页
html = """ <html><head><title>The Dormouse's story</title>< ...
- python学习目录(转载)
python基础篇 python 基础知识 python 初始python python 字符编码 python 类型及变量 python 字符串详解 python 列表详解 ...
- Yeelink初步体验
环境 Qemu: 2.8.0 开发板:vexpress-ca9 概述 前面的博文已经使我们的虚拟开发板具备了访问外网的目的,离物联网越来越近了.要玩物联网,Yeelink不得不说,它提供了 ...
随机推荐
- 自己存档:ajax 动态提交form
$.ajax({ cache: true, type: "POST", ...
- md5加密篇(一)
/// <summary> /// 获取文件的md5摘要 /// </summary> /// <param name="sFile">文件流& ...
- Windows7 x64配置 Apache2 + PHP5 + MySQL5
1:相关软件下载: Apache HTTP Server 版本:(httpd-2.2.25-win32-x86-openssl-0.9.8y) PHP ...
- android之服务
android中的进程优先级 前台进程 拥有一个正在与用户交互的Activity(onResume方法被调用) 与一个前台Activity绑定的服务 服务调用了startForeground onCr ...
- DLL丢失修复
DLL丢失修复,简答傻瓜式! DirectX修复工具(DirectX Repair)是一款系统级工具软件,简便易用.本程序为绿色版,无需安装,可直接运行. 本程序的主要功能是检测当前系统的Dir ...
- CentOS6.6搭建LNMP环境
CentOS6.6搭建LNMP环境 1.设置yum源,本地安装依赖包 1 yum -y install gcc gcc-c++ automake autoconf libtool make 2.下载依 ...
- 基本数据类型-列表_元组_字典_day4
一.列表(list)书写格式:[] #通过list类创建的 li = [1, 12, 9, ", 10, ],"庞麦郎"], "ales", True ...
- 如何配置全世界最小的 MySQL 服务器
配置全世界最小的 MySQL 服务器——如何在一块 Intel Edison 为控制板上安装一个 MySQL 服务器. 介绍 在我最近的一篇博文中,物联网,消息以及 MySQL,我展示了如果 Part ...
- JAVA 一个或多个空格分割字符串
知识补充 String的split方法支持正则表达式: 正则表达式\s表示匹配任何空白字符,+表示匹配一次或多次. 有了以上补充知识,下面的内容就很好理解了. 一.待分割字符串 待分割字符串为如下: ...
- 【USACO 1.3】Combination Lock
/* TASK:combo LANG:C++ URL:http://train.usaco.org/usacoprob2?a=E6RZnAhV9zn&S=combo SOLVE:自己做,想的是 ...