网页解析--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 ...
随机推荐
- django-ForeignKey,OneToOneField,ManyToManyField
进入到django自带的related.py中,可以看到 1.ForeignKey 初始化的参数有: to, on_delete, related_name=None, related_query_n ...
- fenby C语言P21
数据类型 数组名字[个数]: #include <stdio.h> int main(){ int a[8]; float b[9]; char c[10]; return 0;}
- 详解k8s中的liveness和readiness的原理和区别
liveness与readiness的探针工作方式源码解析 liveness和readiness作为k8s的探针,可以对应用进行健康探测. 二者支持的探测方式相同.主要的探测方式支持http探测,执行 ...
- Flask解析(二):Flask-Sqlalchemy与多线程、多进程
Sqlalchemy flask-sqlalchemy的session是线程安全的,但在多进程环境下,要确保派生子进程时,父进程不存在任何的数据库连接,可以通过调用db.get_engine(app= ...
- spring-boot-plus是易于使用,快速,高效,功能丰富,开源的spring boot 脚手架.
Everyone can develop projects independently, quickly and efficiently! spring-boot-plus是一套集成spring bo ...
- Java内存模型(JMM)详解
在Java JVM系列文章中有朋友问为什么要JVM,Java虚拟机不是已经帮我们处理好了么?同样,学习Java内存模型也有同样的问题,为什么要学习Java内存模型.它们的答案是一致的:能够让我们更好的 ...
- ansible模块之command、shell、script、file、copy、fetch
前戏 ansible 批量在远程主机上执行命令 openpyxl 操作excel表格 puppet ansible slatstack ansible epel源 第一步: 下载epel源 wget ...
- 2018.8.15 python 中的sorted()、filter()、map()函数
主要内容: 1.lambda匿名函数 2.sorted() 3.filter() 4.map() 5.递归函数 一. lambda匿名函数 为了解决一些简单的需求而设计的一句话函数 # 计算n的n次方 ...
- Asp.Net Core 单元测试正确姿势
背景 ASP.NET Core 支持依赖关系注入 (DI) 软件设计模式,并且默认注入了很多服务,具体可以参考 官方文档, 相信只要使用过依赖注入框架的同学,都会对此有不同深入的理解,在此无需赘言. ...
- Matplotlib 设置
# 导入相关模块 import matplotlib.pyplot as plt import numpy as np 设置 figure Matplotlib 绘制的图形都在一个默认的 figure ...