网页解析--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 ...
随机推荐
- 如何在Spring Boot中使用Cookies
一. 导读 本文大纲 读取HTTP Cookie 设置HTTP Cookie 读取所有Cookie[] 为Cookie设置过期时间 Https与Cookie HttpOnly Cookie 删除Coo ...
- python实现输入任意一个大写字母生成金字塔的示例
输入任意一个大写字母,生成金字塔图形 def GoldTa(input): L = [chr(i) for i in range(65, 91)] # 大写字母A--Z idA = 65 # 从A开始 ...
- python str的一些操作及处理
一.str的定义:Python中凡是用引号引起来的数据可以称为字符串类型,组成字符串的每个元素称之为字符,将这些字符一个一个连接起来,然后在用引号起来就是字符串. 二.str的简单操作方法: conu ...
- 学习笔记53_C#操作MongoDB
1.配置MongoDB的连接字符串 MongoDB程序集引用 在使用db.GetCollerction<T>,也可以不指定类,因为Mongodb是无模式的. ****关系型数据设计转化为j ...
- 手写一个简易的IOC
这个小项目是我读过一点Spring的源码后,模仿Spring的IOC写的一个简易的IOC,当然Spring的在天上,我写的在马里亚纳海沟,哈哈 感兴趣的小伙伴可以去我的github拉取代码看着玩 地址 ...
- ASP.NET Core Blazor 用Inspinia静态页模板搭建简易后台(实现菜单选中)
Blazor 是一个用于使用 .NET 生成交互式客户端 Web UI 的框架: 使用 C# 代替 JavaScript 来创建丰富的交互式 UI. 共享使用 .NET 编写的服务器端和客户端应用逻辑 ...
- python——inspect模块
inspect模块常用功能 import inspect # 导入inspect模块 inspect.isfunction(fn) # 检测fn是不是函数 inspect.isgenerator((x ...
- Linux 使用记录
作为web程序员,该掌握的 linux 命令有哪些,稍微高级点的? - 刘志军的回答 - 知乎 https://www.zhihu.com/question/64063454/answer/21 ...
- 导入maven项目pom报错
打开window->preferences->maven->user settings更换settings.xml即可
- nyoj 311-完全背包 (动态规划, 完全背包)
311-完全背包 内存限制:64MB 时间限制:4000ms Special Judge: No accepted:5 submit:7 题目描述: 直接说题意,完全背包定义有N种物品和一个容量为V的 ...