吴裕雄--python学习笔记:BeautifulSoup模块
import re
import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.com')
soup = BeautifulSoup(req_obj.text,'lxml') '''标签查找'''
print(soup.title) #只是查找出第一个
print(soup.find('title')) #效果和上面一样
print(soup.find_all('div')) #查出所有的div标签
<title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</title>
<title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</title>
[<div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/><span class="bg s_ipt_wr"><in
'''获取标签里的属性'''
tag = soup.div
print(tag)
# print(tag['class']) #多属性的话,会返回一个列表
print(tag['id']) #查找标签的id属性
print(tag.attrs) #查找标签所有的属性,返回一个字典(属性名:属性值)
<div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/>
'''标签包的字符串'''
tag = soup.title
print(tag.string) #获取标签里的字符串
print(tag.string.replace_with("哈哈")) #字符串不能直接编辑,可以替换
'''子节点的操作'''
tag = soup.head
print(tag.title) #获取head标签后再获取它包含的子标签
<title>哈哈</title>
'''contents 和 .children'''
tag = soup.body
print(tag.contents) #将标签的子节点以列表返回
print([child for child in tag.children]) #输出和上面一样
[' ', <div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/>
'''descendants'''
tag = soup.body
[print(child_tag) for child_tag in tag.descendants] #获取所有子节点和子子节点
<div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/>
'''strings和.stripped_strings'''
tag = soup.body
[print(str) for str in tag.strings] #输出所有所有文本内容
[print(str) for str in tag.stripped_strings] #输出所有所有文本内容,去除空格或空行
'''.parent和.parents'''
tag = soup.title
print(tag.parent) #输出便签的父标签
<head><meta content="text/html;charset=utf-8" http-equiv="content-type"/><meta content="IE=Edge" http-equiv="X-UA-Compatible"/><meta content="always" name="referrer"/><link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/><title>哈哈</title></head>
[print(parent) for parent in tag.parents] #输出所有的父标签
<head><meta content="text/html;charset=utf-8" http-equiv="content-type"/><meta content="IE=Edge" http-equiv="X-UA-Compatible"/><meta content="always" name="referrer"/><link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/><title>哈哈</title></head>
'''.next_siblings 和 .previous_siblings
查出所有的兄弟节点
''' '''.next_element 和 .previous_element
下一个兄弟节点
''' '''find_all的keyword 参数'''
soup.find_all(id='link2') #查找所有包含 id 属性的标签
soup.find_all(href=re.compile("elsie")) #href 参数,Beautiful Soup会搜索每个标签的href属性:
soup.find_all(id=True) #找出所有的有id属性的标签
soup.find_all(href=re.compile("elsie"), id='link1') #也可以组合查找
soup.find_all(attrs={"属性名": "属性值"}) #也可以通过字典的方式查找
吴裕雄--python学习笔记:BeautifulSoup模块的更多相关文章
- 吴裕雄--python学习笔记:sqlite3 模块
1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...
- 吴裕雄--python学习笔记:os模块的使用
在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...
- 吴裕雄--python学习笔记:os模块函数
os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...
- 吴裕雄--python学习笔记:sqlite3 模块的使用与学生信息管理系统
import sqlite3 cx = sqlite3.connect('E:\\student3.db') cx.execute( '''CREATE TABLE StudentTable( ID ...
- 吴裕雄--python学习笔记:爬虫基础
一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...
- 吴裕雄--python学习笔记:爬虫包的更换
python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...
- 吴裕雄--python学习笔记:爬虫
import chardet import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网 ...
- 吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理
import sqlite3 conn = sqlite3.connect('E:\\student.db') print("Opened database successfully&quo ...
- Python学习笔记之模块与包
一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...
随机推荐
- CodeForces 382B 数学推导
这个题目题意简单,但是TLE得哭哭的... 输入 a b w x c五个数,最终要使得c<=a, 每一秒可以进行一个操作,如果b>=x,则 b=b-x,同时 c--;如果b<x,则a ...
- NATAPP内网穿透软件使用指南
1.请求官网路径没有账号的注册,有账号的直接登录 https://natapp.cn 2.下载不同环境所需的启动文件,另存为不同目录 https://natapp.cn/#download --> ...
- win10下安装cygwin全过程
简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...
- 记一次修复Windows
打开了一堆网页和应用,然后桌面不见了.. 于是很着急..就按各种快捷键..Win+R挂了.. 本来以为要reboot(机房电脑) , 然后问老师发现会格式化 然后发现Ctrl+Alt+Delete还存 ...
- Python 学习笔记:根据输入年月区间,返回期间所有的月份
目的: 给定一个年月区间,比如:2019.01 至 2019.05,要求返回一个包含期间所有的月份的列表,比如:['2019.01', '2019.02', '2019.03', '2019.04', ...
- JavaScript学习笔记 - 进阶篇(7)- 浏览器对象
window对象 window对象是BOM的核心,window对象指当前的浏览器窗口. window对象方法: 注意:在JavaScript基础篇中,已讲解了部分属性,window对象重点讲解计时器. ...
- React Native 开发
摘自:<React Native 开发之 IDE 选型和配置> 一个在不断更新的有关React Native讲解:<江清清的技术专栏> ES5和ES6的区别:<React ...
- C#chart图表的应用
在图表中,x轴代表类别,y轴代表数值(好比类与他们的属性) 这是数据库中的数据,下面我们选前5辆车,在图表中显示他们的名字,油耗,功率,价格 创建查询数据的类 class CarDA { public ...
- Matlab高级教程_第二篇:MATLAB和C#对应数据类型的讲解(多讲一点儿C#的矩阵运算)
1. MATLAB对应C#的数据类型主要在引入的父类库MWArray当中.有如下对应规则 .NET TYPE MWArrayTYPE MATLAB Type System.Double MWNumer ...
- 微信公众平台三种IP白名单场景及设置问题
在开发使用微信公众平台时,目前遇到有三处需要配置IP白名单. 1.微信公众平台,“获取access_token”接口新增IP白名单保护,官网:https://mp.weixin.qq.com/cgi- ...