一、 查找a标签

(1)查找所有a标签

>>> for x in soup.find_all('a'):
print(x) <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)查找所有a标签,且属性值href中需要保护关键字“”

>>> for x in soup.find_all('a',href = re.compile('lacie')):
print(x) <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>

(3)查找所有a标签,且字符串内容包含关键字“Elsie”

>>> for x in soup.find_all('a',string = re.compile('Elsie')):
print(x) <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

(4)查找body标签的所有子标签,并循环打印输出

>>> for x in soup.find('body').children:
if isinstance(x,bs4.element.Tag): #使用isinstance过滤掉空行内容
print(x) <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>

二、信息提取(链接提取)

(1)解析信息标签结构,查找所有a标签,并提取每个a标签中href属性的值(即链接),然后存在空列表;

>>> linklist = []
>>> for x in soup.find_all('a'):
link = x.get('href')
if link:
linklist.append(link) >>> for x in linklist: #验证:环打印出linklist列表中的链接
print(x) http://example.com/elsie
http://example.com/lacie
http://example.com/tillie

小结:链接提取 <---> 属性内容提取 <---> x.get('href')

(2)解析信息标签结构,查找所有a标签,且每个a标签中href中包含关键字“elsie”,然后存入空列表中;

>>> linklst = []
>>> for x in soup.find_all('a', href = re.compile('elsie')):
link = x.get('href')
if link:
linklst.append(link) >>> for x in linklst: #验证:循环打印出linklist列表中的链接
print(x) http://example.com/elsie

小结:在进行a标签查找时,加入了对属性值href内容的正则匹配内容 <---> href = re.compile('elsie')

(3)解析信息标签结构,查询所有a标签,然后输出所有标签中的“字符串”内容;

>>> for x in soup.find_all('a'):
string = x.get_text()
print(string) Elsie
Lacie
Tillie

python 之 BeautifulSoup标签查找与信息提取的更多相关文章

  1. python之BeautifulSoup库

    1. BeautifulSoup库简介 和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.lxml 只会局部遍历 ...

  2. 爬虫之标签查找补充及selenium模块的安装及使用与案例

    今日内容概要 bs模块之标签查找 过滤器 selenium模块 今日内容详细 html_doc = """ <html> <head> <t ...

  3. Python实例---beautifulsoup小Demo

    豆瓣 # coding:utf - 8 from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen( ...

  4. Python和BeautifulSoup进行网页爬取

    在大数据.人工智能时代,我们通常需要从网站中收集我们所需的数据,网络信息的爬取技术已经成为多个行业所需的技能之一.而Python则是目前数据科学项目中最常用的编程语言之一.使用Python与Beaut ...

  5. Python Download Image (python + requests + BeautifulSoup)

    环境准备 1 python + requests + BeautifulSoup 页面准备 主页面: http://www.netbian.com/dongman/ 图片伪地址: http://www ...

  6. 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台

    搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...

  7. Python配合BeautifulSoup读取网络图片并保存在本地

    本例为Python配合BeautifulSoup读取网络图片,并保存在本地. BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag.Property等 # ...

  8. python glob 用通配符查找指定目录中的文件 - 开源中国社区

    python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件

  9. python scrapy,beautifulsoup,regex,sgmparser,request,connection

    In [2]: import requests   In [3]: s = requests.Session()   In [4]: s.headers 如果你是爬虫相关的业务?抓取的网站还各种各样, ...

随机推荐

  1. zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

    题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走 ...

  2. Putty使用帐号和密码的自动登录

    Putty使用ssh key做验证登陆是最方便的,不用密码.如果不想做key exchange,只是单纯想保存帐号密码做自动登陆,可以借助bat文件的方式如下,其中MyServer是已经保存了的ses ...

  3. drf的安装和配置

    一.安装 1.安装 pip install djangorestframework 2.配置 注:以上两部就OK了 二.最简单的drf版本 1.创建应用 在项目中新建一个应用: python mana ...

  4. PIE SDK专题制图保存模板

    1.    功能简介 在PIE SDK中,所有的制图元素.视图范围以及排版等都可以保存成一个模板,以供多次重复使用.使用模板时只需要打开该模板,加载相应数据,就可以直接出图,省去了重复制作图幅的麻烦, ...

  5. oracle常用DDL语句

    1.添加表字段--咨询表添加内容简介字段 ALTER TABLE s_table ADD intro VARCHAR2(1024); COMMENT ON COLUMN s_table.remarks ...

  6. Quartz和TopShelf Windows服务作业调度

    上一次写了一遍关于Quartz作业调度的文章 Quartz.NET 作业调度使用 现在使用TopShelf和Quartz实现windows服务作业调度 TopShelf版本4.0 Quartz版本3. ...

  7. linux 入门命令总结

    1,tree -d /etc/ 参数表示只显示目录 -f 显示内容的完整 -i 不显示树枝显示完整路径2,mkdir -p 递归创建多级目录 -v 显示创建目录的过程 -m 设置目录的默认权限 mkd ...

  8. Docker原理(开发技术分享转发)

    Docker原理Docker是啥Docker是一个程序运行.测试.交付的开放平台,Docker被设计为能够使你快速地交付应用.在Docker中,你可以将你的程序分为不同的 基础部分,对于每一个基础部分 ...

  9. zookeeper 节点信息

    使用get命令获取指定节点的数据时, 同时也将返回该节点的状态信息, 称为Stat. 其包含如下字段: czxid. 节点创建时的zxid. mzxid. 节点最新一次更新发生时的zxid. ctim ...

  10. Thinking in java源码下载链接

    Thinking in java书上显示的下载源码到www.mindview.net站点,但是这个站点打不开了,后来找到真正的下载地址,贴于此. http://www.mindviewinc.com/ ...