python 之 BeautifulSoup标签查找与信息提取
一、 查找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标签查找与信息提取的更多相关文章
- python之BeautifulSoup库
1. BeautifulSoup库简介 和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.lxml 只会局部遍历 ...
- 爬虫之标签查找补充及selenium模块的安装及使用与案例
今日内容概要 bs模块之标签查找 过滤器 selenium模块 今日内容详细 html_doc = """ <html> <head> <t ...
- Python实例---beautifulsoup小Demo
豆瓣 # coding:utf - 8 from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen( ...
- Python和BeautifulSoup进行网页爬取
在大数据.人工智能时代,我们通常需要从网站中收集我们所需的数据,网络信息的爬取技术已经成为多个行业所需的技能之一.而Python则是目前数据科学项目中最常用的编程语言之一.使用Python与Beaut ...
- Python Download Image (python + requests + BeautifulSoup)
环境准备 1 python + requests + BeautifulSoup 页面准备 主页面: http://www.netbian.com/dongman/ 图片伪地址: http://www ...
- 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台
搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...
- Python配合BeautifulSoup读取网络图片并保存在本地
本例为Python配合BeautifulSoup读取网络图片,并保存在本地. BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag.Property等 # ...
- python glob 用通配符查找指定目录中的文件 - 开源中国社区
python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件
- python scrapy,beautifulsoup,regex,sgmparser,request,connection
In [2]: import requests In [3]: s = requests.Session() In [4]: s.headers 如果你是爬虫相关的业务?抓取的网站还各种各样, ...
随机推荐
- RPC 框架 应用
RPC RPC(Remote Procedure Call)服务,也即远程过程调用,在互联网企业技术架构中占据了举足轻重的地位,尤其在当下微服务化逐步成为大中型分布式系统架构的主流背景下,RPC 更扮 ...
- 石头剪刀布(2019Wannafly winter camp day3 i) 带权并查集+按秩合并 好题
题目传送门 思路: 按照题意描述,所有y挑战x的关系最后会形成一棵树的结构,n个人的总方案数是 3n 种,假设一个人被挑战(主场作战)a次,挑战别人(客场)b次,那么这个人存活到最后的方案数就是3n* ...
- C# 关于utf-8的研究
前提 如果一不小心把字符转成utf8的格式,但是却产生了乱码.这个时候要么就是寻找其他的转码方式,要么就不想要了,直接过滤吧. 这里说的是直接过滤的办法. 参考链接 https://netvignet ...
- SPOJ - DQUERY 莫队
题意:给定\(a[1...n]\),\(Q\)次询问,每次统计\([L,R]\)范围内有多少个不同的数字 xjb乱写就A了,莫队真好玩 #include<iostream> #includ ...
- POJ - 3233 矩阵套矩阵
题意:给你矩阵\(A\),求\(S=\sum_{i=1}^{k}A^i\) 构造矩阵 \[ \begin{bmatrix} A & E \\ 0 & E\\ \end{bmatrix} ...
- POJ - 2676 暴搜 注意实现细节
经典sudoku问题 按部就班就好 一定要注意细节 大于1还是大于等于1 r c越界判断 judge时0的特判 blabla居然磨了2个小时 改了很多地方所以实现得有点冗余,反正能A吧 /*H E A ...
- springboot(三)-使用JSP
Springboot的默认视图支持是Thymeleaf.这里先不谈,这么优秀的框架怎么可能不能使用JSP呢?不允许的. 那么需要添加对jsp的支持. pom.xml 在pom.xml文件中添加依赖 & ...
- JS window,onload 与 $().read()
JS:window.onload的使用介绍 .在body标签里面 .在JS语句调用 .同时调用多个函数 .JS调用多个函数 .自定义的函数多次调用 jquery $(document).ready() ...
- python--交互器,编译器
1在写python的时候调交互器的作用 唯一作用:调试代码 2.编译器
- PIE SDK应用掩膜
1.算法功能简介 当对一幅图像应用掩膜时, 1 值的区域被保留, 0 值的区域被舍弃( 1 值区域被处理, 0 值区域被屏蔽不参与计算). PIE SDK支持算法功能的执行,下面对应用掩膜算法功能进行 ...