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模块的更多相关文章

  1. 吴裕雄--python学习笔记:sqlite3 模块

    1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...

  2. 吴裕雄--python学习笔记:os模块的使用

    在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...

  3. 吴裕雄--python学习笔记:os模块函数

    os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...

  4. 吴裕雄--python学习笔记:sqlite3 模块的使用与学生信息管理系统

    import sqlite3 cx = sqlite3.connect('E:\\student3.db') cx.execute( '''CREATE TABLE StudentTable( ID ...

  5. 吴裕雄--python学习笔记:爬虫基础

    一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...

  6. 吴裕雄--python学习笔记:爬虫包的更换

    python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...

  7. 吴裕雄--python学习笔记:爬虫

    import chardet import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网 ...

  8. 吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理

    import sqlite3 conn = sqlite3.connect('E:\\student.db') print("Opened database successfully&quo ...

  9. Python学习笔记之模块与包

    一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...

随机推荐

  1. .jar文件不能解析、识别

  2. C/C++ 取整函数ceil(),floor()

    使用floor函数.floor(x)返回的是小于或等于x的最大整数.如:     floor(10.5) == 10    floor(-10.5) == -11 使用ceil函数.ceil(x)返回 ...

  3. C++常用库函数 C函数库 cstdio

    常用的C/C++函数库, cstdio(stdio.h) 标准输入输出库.C Standard Input and Output Library 1. 实例 #include <cstdio&g ...

  4. 用Plotily处理数据的基本操作

    import pandas as pd # 导入数据.scv df = pd.read_csv(" .csv") # 查看前五行数据 df.head() # 查看一下数据描述 df ...

  5. JavaScript中Promise 使用、原理以及实现过程

    1.什么是 Promise promise 是目前 JS 异步编程的主流解决方案,遵循 Promises/A+ 方案. 2.Promise 原理简析 (1)promise 本身相当于一个状态机,拥有三 ...

  6. msgfmt - 翻译汉化

    说明 目前大部分自由软件实现国际化使用的是gettext. 国际化就是让程序可以使用多国语言来显示程序里的字符串. 程序里一般都有很多字符串,菜单名也好,错误信息也好,都是字符串.假设字符串为stri ...

  7. Navicat-pymysql-sql注入问题

    一.Navicat 可视化工具的使用 1.Navicat [1]  是一套快速.可靠并价格相宜的数据库管理工具,专为简化数据库的管理及降低系统管理成本而设. 它的设计符合数据库管理员.开发人员及中小企 ...

  8. 大厂面试题:今天复试百度PHP工程师

    今天下午来到北京百度科技园进行复试PHP工程师岗位. 面试官问了很多问题,我大概整理回忆下: 1.Redis秒杀实现? redis队列解决抢购高并发的原理: 在程序跟数据库之前呢我们可以利用redis ...

  9. 定时任务--Timer()实现

    Java的Timer以及TimerTask类可以帮助我们实现定时器功能,利用servlet监听程序可以实现WEB服务启动之后执行某些工作.两者结合就可以再web应用中实现定时器功能. 1.计划类代码S ...

  10. MTSP问题

    问题描述:m个旅行商去旅游 n个城市,规定都必须从同一个出发点出发,而且返回原出发点,需要将所有的城市遍历完毕,每个城市只能游历一次,但是为了路径最短可以路过这个城市多次.这个就是多旅行商问题.是在T ...