网页解析--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 ...
随机推荐
- Zookeeper与HBase的安装
一.Zookeeper的安装 1.http://www-us.apache.org/dist/zookeeper/stable/下载Zookeeper安装包,并将zookeeper-3.4.12.ta ...
- seq2seq+attention解读
1什么是注意力机制? Attention是一种用于提升Encoder + Decoder模型的效果的机制. 2.Attention Mechanism原理 要介绍Attention Mechanism ...
- windows系统先安装hexo
一.安装node.js 下载地址为:https://nodejs.org/en/可以根据自己需要下载对于的版本. 打开cmd,输入指令 node -v 若出现上图这样的结果则说明安装好了. 二.安装h ...
- Ubuntu16.04下升级Python到3.6
转: 这里 有一篇帖子是说从源代码开始安装,这种方式原来尝试过,需要删除系统默认的软链命令,感觉比较粗暴,现在在想有没有更好的方式呢? 找到一个帖子:http://ubuntuhandbook.org ...
- Java基础学习框架总结
内容:Java基础知识全面复习 时间:2019.9.3-2019.9.26 代码:D:/ProgramFiles/IDEA/hello_sort 一.基础知识 learning1 case分支 Inp ...
- 不需要怎么修改配置的Mybatis整合Spring要点
首先对于Mybatis的主配置文件,只需要修改一处地方,将事务交给Spring管理,其它地方可以原封不动. <?xml version="1.0" encoding=&quo ...
- php 下载图片并打包成Zip格式压缩包
前言:最近公司有个需要下载多个图片并打包成压缩包的需求,下面来看看具体是怎么做的 1.没什么说的,懒得说啥,直接看代码 /** * 下载图片并生成压缩包 * @param $data 图片数组,一维 ...
- 手把手带你实战下Spring的七种事务传播行为
目录 本文目录 一.什么是事务传播行为? 二.事务的7种传播行为 三.7种传播行为实战 本文介绍Spring的七种事务传播行为并通过代码演示下. 本文目录 一.什么是事务传播行为? 事务传播行为(pr ...
- (转)白话数字签名(2)——软件&设备
然而它太慢了 非对称加密算法有一个重大缺点——加密速度慢,或者说得更拽一些,编码率比较低.例如在上一篇里我给Clark传的那个1GB的小电影,进行非对称加密足足用了66小时.那个借条小一些吧,也用了将 ...
- (C#)WPF:Property和Attribute的区别
在C#里Property是属性,Attribute是特性.它们的概念是不一样的,充其量就是中文的神翻译问题. 1)属性是指类体里用get或set封装好的属性.属性是面向对象的理论范畴.比如说一个盒子, ...