iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python爬虫系列三:html解析大法
  • 371
分享到

python爬虫系列三:html解析大法

爬虫大法系列 2023-01-31 07:01:42 371人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

Beautiful Soup 是一个可以从html或XML文件中提取数据的python库。 它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。 在爬虫开发中主要用的是Beautiful Soup的查

Beautiful Soup 是一个可以从html或XML文件中提取数据的python库。
它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。
爬虫开发中主要用的是Beautiful Soup的查找提取功能。
Beautiful Soup是第三方模块,需要额外下载
下载命令:pip install bs4
安装解析器:pip install lxml
这里写图片描述
这里写图片描述
这里写图片描述

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>
"""
#创建一个bs对象
#默认不指定的情况,bs会选择Python内部的解析器
#因此指定lxml作为解析器
soup=BeautifulSoup(html_doc,"lxml")
----------

 1. 解析网页后的类型及格式化

print(type(soup))  #<class 'bs4.BeautifulSoup'>
print(soup.prettify())  #格式化 答案如下:

<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>

 2. 基本操作

#1.标签选择法 缺点:只能猎取到符合条件的第一个
#(1)获取p标签
print(soup.p) #<p class="title"><b>The Dormouse's story</b></p>
print(soup.a) #<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
#(2)获取a标签的herf属性:因为标签下的内容是以键对形式体现出来
print(soup.a["href"]) #http://example.com/elsie
#(3)获取标签的内容
print(soup.title.string) #The Dormouse's story
print(soup.a.string)#Elsie


----------
3.遍历文档树

#1.操作子节点:contents ,children(子节点)
print(soup.body.contents) #得到文档对象中body中所有的子节点,返回的列表,包括换行符
print(soup.body.children) #<list_iterator object at 0x0000000002ACC668>
#这个意思是意味着返回的是列表迭代器,需要用for 将列表中的内容遍历出来
for i in soup.body.children:
    print(i)

answer:
<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>

#2.操作父节点:parent(父节点),parents(祖父节点)

print(soup.a.parent)
print(soup.a.parents)

<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>

#兄弟节点
next_siblings 
next_sibling
previous_siblings 
previous_sinbling
----------
4、搜索文档树

#搜索文档树
#(1)查询指定的标签
res1=soup.find_all("a")
print(res1) #返回所有的a标签,返回列表

answer:
[<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>]
#(2)正则表达式
#compile():声明正则表达式对象
#查找包含d字符的便签
import re
res2=soup.find_all(re.compile("d+"))
print(res2)

answer:
[<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>]
#(3)列表
# 查询所有title标签或者a标签
res3=soup.find_all(["title","a"])
print(res3)

answer:
[<title>The Dormouse's story</title>, <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>]
#(4)关键字参数
#查询属性id="link2"的标签
res4=soup.find_all(id="link2")
print(res4)

answer:
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

#(5)内容匹配
res5=soup.find_all(text="Elsie")
print(res5)

answer:
['Elsie']

find_all()
find():返回的匹配结果的第一个元素
find_parents()               find_parent()
find_next_siblings()         find_next_sibling()
find_previous_siblings()     find_previous_sibling()
find_all_next()              find_next()
find_all_previous()          find_previous()


----------
5.CSS选择器

使用CSS选择器,只需要调用SeleCt()方法,传入相应的CSS选择器,返回类型是 liSt

CSS语法:

 - 标签名不加任何修饰
 - 类名前加点
 - id名前加 #
 - 标签a,标签b : 表示找到所有的节点a和节点c
 - 标签a 标签b : 表示找到节点a下的所有节点b
 - get_text() :获取节点内的文本内容

#(1)通过id查询标签对象
res2=soup.select("#link3")
print(res2)
#[<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

#(2)根据class属性查询标签对象
res3=soup.select(".sister")
print(res3)

#(3)属性选择
res4=soup.select("a[herf='http://example.com/elsie']")
print(res4)

#(4)包含选择器
res5=soup.select("p a#link3")#p标签下的a标签下的link3
print(res5)
answer:
[<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

#(5)得到标签内容
res6=soup.select("p a.sister")
print(res6)
#获取标下下的内容
print(res6[0].get_text())
print(res6[0].string)

answer:
[<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>]
Elsie
Elsie



--结束END--

本文标题: python爬虫系列三:html解析大法

本文链接: https://www.lsjlt.com/news/191891.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python爬虫系列三:html解析大法
    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库。 它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。 在爬虫开发中主要用的是Beautiful Soup的查...
    99+
    2023-01-31
    爬虫 大法 系列
  • Python爬虫之解析HTML页面详解
    目录用Python解析HTML页面HTML 页面的结构XPath 解析CSS 选择器解析正则表达式解析总结用Python解析HTML页面 在网络爬取的过程中,我们通常需要对所爬取的页...
    99+
    2023-05-18
    Python解析HTML Python爬虫HTML
  • Python爬虫系列(一)——手把手教你写Python爬虫
    1. 什么是爬虫? 根据百度百科的定义,网络爬虫,又称为网页蜘蛛,是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。 人们如今的生活,大都离不开网络,发一条微信,电子支付买一杯奶茶,刷一条微博...
    99+
    2023-09-14
    爬虫 python 数据挖掘
  • python爬虫之三:解析网络报文xml
    本节主要是讲解在项目中怎么解析获取的xml报文并获取相关字段。 xml解析第三方库学习地址:http://www.runoob.com/python/python-xml.html xml文件如下: <xml versio...
    99+
    2023-01-31
    报文 爬虫 之三
  • Python爬虫系列 - 初探:爬取新闻
    Get发送内容格式 Get方式主要需要发送headers、url、cookies、params等部分的内容。 t = requests.get(url, headers = header, params = content, cooki...
    99+
    2023-01-30
    爬虫 系列 新闻
  • java爬虫jsoup解析HTML实例分析
    本篇内容介绍了“java爬虫jsoup解析HTML实例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!前言使用python写爬虫的人,应该...
    99+
    2023-07-02
  • Python爬虫的两套解析方法和四种爬虫实现
    【本文转载自微信公众号:数据科学家养成记,作者:louwill,转载授权请联系原作者】 对于大多数朋友而言,爬虫绝对是学习python的最好的起手和入门方式。因为爬虫思维模式固定,编程模式也相对简单,一般在细节处理上积累一些经验都...
    99+
    2023-06-02
  • python爬虫系列网络请求案例详解
    目录urllib的介绍urllib库的四大模块:案例发送请求参数说明:代码案例发送请求-Request请求IP代理IP代理分类:使用cookie使用步骤:异常处理学习了之前的基础和爬...
    99+
    2024-04-02
  • Python爬虫实战之xpath解析
    XPath 是一门在 XML 文档中查找信息的语言,最初是用来搜寻 XML 文档的,但是它同样适用于 HTML 文档的搜索。 所以在Python爬虫中,我们经常使用xpath解析这种高效便捷的方式来提...
    99+
    2023-09-23
    python 爬虫 开发语言
  • java爬虫jsoup解析HTML的工具学习
    目录前言下载一个文档的对象模型获取 Document 对象解析并提取 HTML 元素使用传统的操作DOM的方式选择器修改获取数据前言 使用python写爬虫的人,应该都听过beaut...
    99+
    2024-04-02
  • Python Ajax爬虫方法案例分析
    今天小编给大家分享一下Python Ajax爬虫方法案例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。1. 抓...
    99+
    2023-06-29
  • Python爬虫笔记3-解析库Xpat
    当爬取到Html数据后,可以用正则对数据进行提取,但有时候正则表达式编写起来不方便,而且万一写错了,可能导致匹配失败。这时候就需要借助其他解析工具了。 XML引入 什么是XML? XML 指可扩展标记语言(EXtensible Mark...
    99+
    2023-01-31
    爬虫 笔记 Python
  • Python爬虫,获取,解析,存储详解
    目录1.获取数据2.解析数据3.数据保存为CSV格式和存入数据库总结1.获取数据 import requests def drg(url): try: h...
    99+
    2024-04-02
  • 【Python爬虫】数据解析之bs4解析和xpath解析
    🔥一个人走得远了,就会忘记自己为了什么而出发,希望你可以不忘初心,不要随波逐流,一直走下去🎶 🦋 欢迎关注🖱点赞👍收...
    99+
    2023-09-06
    python 爬虫 开发语言
  • python爬虫教程之bs4解析和xpath解析详解
    目录bs4解析原理:如何实例化BeautifulSoup对象:用于数据解析的方法和属性:xpath解析xpath解析原理:实例化一个etree对象:xpath( ‘xpa...
    99+
    2024-04-02
  • Python爬虫网页,解析工具lxml.html(二)
    【前情回顾】如何灵活的解析网页,提取我们想要的数据,是我们写爬虫时非常关心和需要解决的问题。从Python的众多的可利用工具中,我们选择了lxml的,它的好我们知道,它的妙待我们探讨。前面我们已经从HTML字符串转换成的HtmlElemen...
    99+
    2023-06-02
  • Python 爬虫网页,解析工具lxml.html(一)
    狭义上讲,爬虫只负责抓取,也就是下载网页。而实际上,爬虫还要负责从下载的网页中提取我们想要的数据,即对非结构化的数据(网页)进行解析提取出结构化的数据(有用数据)。比如,我们要抓取了一个新闻页面的网页(html)下来,但我们想要的是这个网页...
    99+
    2023-06-02
  • python爬虫指南之xpath实例解析
    Python爬虫指南之XPath实例解析XPath是一种用于在XML文档中进行导航和查找元素的语言。在爬虫中,XPath可以用于解析...
    99+
    2023-08-15
    python
  • Python爬虫解析器BeautifulSoup4怎么使用
    这篇文章主要介绍“Python爬虫解析器BeautifulSoup4怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python爬虫解析器BeautifulSoup4怎么使用”文章能帮助大家解...
    99+
    2023-07-02
  • python爬虫爬取指定内容的解决方法
    目录解决办法:实列代码如下:(以我们学校为例)爬取一些网站下指定的内容,一般来说可以用xpath来直接从网页上来获取,但是当我们获取的内容不唯一的时候我们无法选择,我们所需要的、所指...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作