网页解析--BeautifulSoup练习
# coding = utf-8
# BeautifulSoup 主要功能是解析提取HTML数据
# re lxml bs4 # pip install Beautifulsoup4 # from bs4 import BeautifulSoup html = '''
<html><head><title>The Dormouse's story</title></head> <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部分
############################################################################# # soup = BeautifulSoup(html, 'lxml') # 四大对象种类:Tag NavigableString Beautifulsoup Comment # print(soup.a) # 获取a标签
# print(soup.a.get('href')) # 取a标签的属性,获得超链接
# print(soup.a.text) # 获取a标签下的文本,若a下有子标签,可能获取不到
# print(soup.a.string) # 获取a标签(包含a下的子标签)下的文本 # 搜索文档:find find_all 按照一定的过滤条件进行匹配 # 字符串
# print(soup.find_all('a')) # 匹配整个文档中的a标签
# print(soup.find_all(attrs={'class': 'title'})) # 匹配class为title的标签 # #正则表达式
# import re
# print(soup.find_all(re.compile('^p'))) # 匹配以p开头的标签
# print(soup.find_all(re.compile('y$'))) # 匹配以y结尾的标签
# print(soup.find_all(re.compile('t'))) # 匹配包含t的标签 # 列表
# for tag in soup.find_all(['a', 'b']): # 匹配a标签,b标签
# print(tag) # for tag in soup.find_all('p', class_='story'): # 匹配class=story的p标签
# print(tag) # # 方法 给find_all传入一个方法作为过滤条件
# def has_class_but_no_id(tag):
# """
# 定义一个判断有class属性但是没有id属性的方法,作为过滤条件
# """
# return tag.has_attr('class') and not tag.has_attr('id')
#
# for tag in soup.find_all(has_class_but_no_id):
# print(tag) # css选择器
# print(soup.select('title')) # 通过标签名查找
# print(soup.select('.sister')) # 通过class名查找
# print(soup.select('#link1')) # 通过id名查找
# print(soup.select('p #link2')) # 组合查找,id为link2的p标签 # > 只能够一级一级向下查找
# print(soup.select('body > p .sister')) # 查找body下类名为sister的p # 百度搜索python,对返回页面进行属性查找
# import requests
# url = 'http://www.baidu.com/s?wd=python'
# response = requests.get(url) # 获取的数据是网页源代码,未经过js渲染
#
# soup = BeautifulSoup(response.text, 'lxml') # 查找返回页面搜索到的结果
# items = soup.find_all('div', class_='result c-container ') # 打印搜索结果
# for item in items:
# print(item.select('h3 > a')[0].get('href') # 取a标签
# print(item.select('h3 > a')[0].get_text()) #################################################################################
# xpath 部分
# 通配符 / // @ # . ..
# /表示从当前节点匹配 //整个文档匹配 @选取属性 *
########################################################################################
html = '''
<html><head><title>The Dormouse's story</title></head>
<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>
'''
# from lxml import etree
# e = etree.HTML(html)
# for i in e.xpath('//p'): # 整个文档中搜索p标签
# # print(i.xpath('string(.)')) # 获取当前标签下所有文本(标签下套标签),包括下面子标签的文本
# print(i.text) # 匹配当前标签下的文本内容,不包含子标签 """
# for i in e.xpath('//p/@class'): # 选取p的class属性
# for i in e.xpath('//p[@class=title]'): # 搜索class=title的p标签
//title[@*] 匹配所有有属性的title标签
"""
# 百度搜索python,用xpath查找
import requests
from lxml import etree url = 'http://www.baidu.com/s?wd=python'
response = requests.get(url) # 获取的数据是网页源代码
tree = etree.HTML(response.text) # 查找返回页面搜索到的结果
items = tree.xpath('//div[@class="result c-container "]')
for item in items:
# print(item.xpath('h3/a/@href'))
print(item.xpath('h3/a')[0].xpath('string(.)'))
网页解析--BeautifulSoup练习的更多相关文章
- 关于爬虫中常见的两个网页解析工具的分析 —— lxml / xpath 与 bs4 / BeautifulSoup
http://www.cnblogs.com/binye-typing/p/6656595.html 读者可能会奇怪我标题怎么理成这个鬼样子,主要是单单写 lxml 与 bs4 这两个 py 模块名可 ...
- 【Python爬虫】BeautifulSoup网页解析库
BeautifulSoup 网页解析库 阅读目录 初识Beautiful Soup Beautiful Soup库的4种解析器 Beautiful Soup类的基本元素 基本使用 标签选择器 节点操作 ...
- 转:Python网页解析:BeautifulSoup vs lxml.html
转自:http://www.cnblogs.com/rzhang/archive/2011/12/29/python-html-parsing.html Python里常用的网页解析库有Beautif ...
- 第6章 网页解析器和BeautifulSoup第三方插件
第一节 网页解析器简介作用:从网页中提取有价值数据的工具python有哪几种网页解析器?其实就是解析HTML页面正则表达式:模糊匹配结构化解析-DOM树:html.parserBeautiful So ...
- python网络爬虫之解析网页的BeautifulSoup(爬取电影图片)[三]
目录 前言 一.BeautifulSoup的基本语法 二.爬取网页图片 扩展学习 后记 前言 本章同样是解析一个网页的结构信息 在上章内容中(python网络爬虫之解析网页的正则表达式(爬取4k动漫图 ...
- 网页解析:Xpath 与 BeautifulSoup
1. Xpath 1.1 Xpath 简介 1.2 Xpath 使用案例 2. BeautifulSoup 2.1 BeautifulSoup 简介 2.2 BeautifulSoup 使用案例 1) ...
- Beautifulsoup网页解析——爬取豆瓣排行榜分类接口
我们在网页爬取的过程中,会通过requests成功的获取到所需要的信息,而且,在返回的网页信息中,也是通过HTML代码的形式进行展示的.HTML代码都是通过固定的标签组合来实现页面信息的展示,所以,最 ...
- Python网页解析
续上篇文章,网页抓取到手之后就是解析网页了. 在Python中解析网页的库不少,我最开始使用的是BeautifulSoup,貌似这个也是Python中最知名的HTML解析库.它主要的特点就是容错性很好 ...
- Python 网页解析器
Python 有几种网页解析器? 1. 正则表达式 2.html.parser (Python自动) 3.BeautifulSoup(第三方)(功能比较强大) 是一个HTML/XML的解析器 4.lx ...
随机推荐
- SpringBoot与MybatisPlus3.X整合之通用枚举(十二)
一 通用枚举 解决了繁琐的配置,让 mybatis 优雅的使用枚举属性! 自3.1.0开始,可配置默认枚举处理类来省略扫描通用枚举配置 默认枚举配置 升级说明: 3.1.0 以下版本改变了原生默认行为 ...
- R语言:绘制知识图谱
知识图谱主要是通过将应用数学,图形学,信息可视化技术,信息科学等学科的理论与方法与计量学引文分析.共现分析等方法结合,利用可视化的图谱形象地展示学科的核心结构.发展历史.前沿领域以及整体知识架构达到多 ...
- Java 基本数据类型的取值范围
基本数据类型,字节数,位数,最大值和最小值. 1. 基本类型:short 二进制位数:16 包装类:java.lang.Short 最小值:Short.MIN_VALUE=-32768 (-2的15此 ...
- 【IDEA】IDEA自动生成文档注释的设置方法
Digest:今天和大家分享一下如何使用IntelliJ IDEA快速生成文档注释 IntelliJ IDEA创建自定义文档注释模板 1.打开IntelliJ IDEA,依次点击 File --> ...
- java架构之路-(MQ专题)RocketMQ从入坑到集群详解
这次我们来说说我们的RocketMQ的安装和参数配置,先来看一下我们RocketMQ的提出和应用场景吧. 早在2009年,阿里巴巴的淘宝第一次提出了双11购物狂欢节,但是在2009年,服务器无法承受到 ...
- activeMQ 安装及启动异常处理
一.环境: [root@centos_6 ~]# cat /etc/system-release CentOS release 6.5 (Final) [root@centos_6 ~]# uname ...
- python中小整数对象池及intern机制
小整数对象池: Python为了优化速度,使用了小整数对象池,避免为整数频繁申请和销毁 Python 对小整数的定义是 [-5, 256] 这些整数对象是提前建立好的,不会被垃圾回收,所有位于这个范围 ...
- NOIP模拟测试9
又考崩了咕咕咕... T1:随 好题标记 前置芝士: 原根:质数P的原根g满足1<=rt<P,且rt的1次方,2次方…(P-1)次方在模P意义下可以取遍1到(P-1)的所有整数. ...
- 使用Typescript重构axios(十一)——接口扩展
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- 基于@Scheduled注解的Spring定时任务
1.创建spring-task.xml 在xml文件中加入命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...