一、信息提取实例

提取HTML中所有的URL链接

思路:1)搜索到所有的<a>标签

   2)解析<a>标签格式,提取href后的链接内容

>>> import requests
>>> r= requests.get("https://python123.io/ws/demo.html")
>>> demo=r.text
>>> demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
>>> from bs4 import BeautifulSoup

soup=BeautifulSoup(demo,'html.parser')

>>> print(soup.prettify())
<html>
<head>
<title>
This is a python demo page
</title>
</head>
<body>
<p class="title">
<b>
The demo python introduces several python courses.
</b>
</p>
<p class="course">
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
Basic Python
</a>
and
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
Advanced Python
</a>
.
</p>
</body>
</html>

>>> for link in soup.find_all('a'):
... print(link.get("href"))
...
http://www.icourse163.org/course/BIT-268001
http://www.icourse163.org/course/BIT-1001870001

二、基于bs4库的HTML内容查找方法

<>.find_all(name,attrs,recursive,string,**kwargs)可以在soup的变量中去查找里面的信息

返回一个列表类型,存储查找的结果

1、name:对标签名称的检索字符串

>>> soup.find_all('a')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
>>> soup.find_all(['a','b'])
[<b>The demo python introduces several python courses.</b>, <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
>>> for tag in soup.find_all(True):  #如果给出的标签名称是True,将显示当前soup的所有标签信息
... print(tag.name)
...
html
head
title
body
p
b
p
a
a
>>> import re

>>> for tag in soup.find_all(re.compile('b')):  #正则表达式库所反馈的结果是指以b开头的所有的信息作为查找的要素
... print(tag.name)
...
body
b

2、attrs:对标签属性值的检索字符串,可标注属性检索

>>> soup.find_all('p','course')
[<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>]

>>> soup.find_all(id='link1')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]
>>> soup.find_all(id='link')
[]
>>> import re
>>> soup.find_all(id=re.compile('link'))
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]

3、recursive:是否对子孙全部检索,默认True

>>> soup.find_all('a')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
>>> soup.find_all('a',recursive=False)
[]

说明从soup根节点开始,他的儿子节点层面上是没有a标签的,a标签应该在子孙的后续节点

4、string:<>...</>中字符串区域的检索字符串

>>> soup
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
>>> soup.find_all(string = "Basic Python")
['Basic Python']
>>> import re
>>> soup.find_all(string=re.compile("python"))
['This is a python demo page', 'The demo python introduces several python courses.']
>>>

<tag>(..) 等价于 <tag>.find_all(..)

soup(..)等价于soup.find_all(..)

七个扩展方法

<>.find()

<>.find_parents()

<>.find_parent()

<>.find_next_siblings()

<>.find_next_sibling()

<>.find_previous_siblings()

<>.find_previous_sibling()

基于bs4库的HTML内容查找方法的更多相关文章

  1. 基于bs4库的HTML标签遍历方法

    基于bs4库的HTML标签遍历方法 import requests r=requests.get('http://python123.io/ws/demo.html') demo=r.text HTM ...

  2. 基于bs4库的HTML查找方法

    基于bs4库的HTML查找方法 find_all方法 <>.find_all(name,attrs,recursive,string,**kwargs) 返回一个列表类型,内部存储查找的结 ...

  3. 基于BeautifulSoup库的HTML内容的查找

    一.BeautifulSoup库提供了一个检索的参数: <>.find_all(name,attrs,recursive,string,**kwargs),它返回一个列表类型,存储查找的结 ...

  4. python bs4库

    Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...

  5. 第14.11节 Python中使用BeautifulSoup解析http报文:使用查找方法快速定位内容

    一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>介绍了BeautifulSoup对象的主要属性,通过这些属性可以访 ...

  6. linux系统中批量查找文件与文件内容的方法

    在linux中查看与修改文件权限我们都必须使用命令来操作,不能像windows一样点几下就好了,下面我们简单的介绍一下linux中的相关命令 比如查找当前目录下面所有的php文件里面某个关键字 fin ...

  7. VBA 根据Find方法根据特定内容查找单元格

    http://club.excelhome.net/thread-940744-1-1.html 2. Find方法的语法[语法]<单元格区域>.Find (What,[After],[L ...

  8. 《爬虫学习》(四)(使用lxml,bs4库以及正则表达式解析数据)

    1.XPath: XPath(XML Path Language)是一门在XML和HTML文档中查找信息的语言,可用来在XML和HTML文档中对元素和属性进行遍历. 工具:扩展商店里搜索:XPath ...

  9. Python 每日提醒写博客小程序,使用pywin32、bs4库

    死循环延迟调用方法,使用bs4库检索博客首页文章的日期是否与今天日期匹配,不匹配则说明今天没写文章,调用pywin32库进行弹窗提醒我写博客.

随机推荐

  1. path('<int:question_id>/vote/', views.vote, name='vote')中的<int:question_id>的含义

    path('<int:question_id>/vote/', views.vote, name='vote')<int:question_id>用于匹配URL的值,并将扑捉到 ...

  2. robotframework常见报错记录

    1.'Response' object has no attribute '__getitem__':'Response'对象没有属性' getitem__' response对象的属性r.statu ...

  3. day22 用户添加相关命令 特殊权限为 防止root修改文件

    2) 用户相关的命令 useradd -u -g -G -M -s -c usermod -u -g -G -s -c userdel -r groupadd groupmod groupdel ch ...

  4. oracle 启动

    Oracle数据库启动过程及状态详解(nomount.mount和open) 先来简要了解一下Oracle数据库体系架构以便于后面深入理解,Oracle Server主要由实例(instance)和数 ...

  5. 第四篇,JavaScript面试题汇总

    JavaScript是一种属于网络的脚本语言,已经被广泛用于web实用开发,常用来为网页添加各种各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HTML中来实现 ...

  6. No Delegate set : lost message:libpng error: Not a PNG file

    当出现这个问题时,是因为本来是jpg或其他格式的图片存成了png导致的.或者有的图片本来就是jpg的,Android Studio一编译,发现不是png才造成了这个问题.解决这个问题可以在Androi ...

  7. Python_装饰器函数

    楔子 作为一个会写函数的python开发,我们从今天开始要去公司上班了.写了一个函数,就交给其他开发用了. def func1(): print('in func1') 季度末,公司的领导要给大家发绩 ...

  8. Nginx中常见问题与错误处理

    1.400 bad request错误的原因和解决办法 配置nginx.conf相关设置如下. client_header_buffer_size 16k;large_client_header_bu ...

  9. CentOS6.5-6.9安装 docker

    安装docker yum -y install docker-io 备注:查看内核版本uname -r ;卸载docker版本命令 yum remove docker 更改配置文件 vim /etc/ ...

  10. win10 解决.net framework 3.5 安装报错 0x80240438

    打开注册表:cmd+r 输入regedit,确定:找到路径HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU ...