转载: https://www.cnblogs.com/chimeiwangliang/p/8649003.html
from bs4 import BeautifulSoup
import requests,re
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标签 '''获取标签里的属性'''
tag = soup.div
print(tag['class']) #多属性的话,会返回一个列表
print(tag['id']) #查找标签的id属性
print(tag.attrs) #查找标签所有的属性,返回一个字典(属性名:属性值) '''标签包的字符串'''
tag = soup.title
print(tag.string) #获取标签里的字符串
tag.string.replace_with("哈哈") #字符串不能直接编辑,可以替换 '''子节点的操作'''
tag = soup.head
print(tag.title) #获取head标签后再获取它包含的子标签 '''contents 和 .children'''
tag = soup.body
print(tag.contents) #将标签的子节点以列表返回
print([child for child in tag.children]) #输出和上面一样 '''descendants'''
tag = soup.body
[print(child_tag) for child_tag in tag.descendants] #获取所有子节点和子子节点 '''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)               #输出便签的父标签
[print(parent) for parent in tag.parents] #输出所有的父标签 '''.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={"属性名": "属性值"}) #也可以通过字典的方式查找

  

(转载) python3: beautifulsoup的使用的更多相关文章

  1. python3 BeautifulSoup模块

    一.安装下载: 1.安装: pip install beautifulsoup4 2.可选择安装解析器:pip install lxmlpip install html5lib 3.解析器比较: 解析 ...

  2. [转载]Python3编码问题详解

    原文:Python3的编码问题 Python3 最重要的一项改进之一就是解决了 Python2 中字符串与字符编码遗留下来的这个大坑.Python 编码为什么那么蛋疼?已经介绍过 Python2 字符 ...

  3. python3 BeautifulSoup模块使用

    BeautifulSoup就是Python的一个HTML或XML的解析库,可以用它来方便地从网页中提取数据.官方解释如下: Beautiful Soup提供一些简单的.Python式的函数来处理导航. ...

  4. Python3 BeautifulSoup和Pyquery解析库随笔

    BeautifuSoup和Pyquery解析库方法比较 1.对象初始化: BeautifySoup库: from bs4 import BeautifulSoup html = 'html strin ...

  5. python3 Beautifulsoup <class 'bs4.element.ResultSet'> <class 'bs4.element.Tag'> 取值

    1.<class 'bs4.element.ResultSet'>  这里是字典外套了一个列表  textPid = pid[0] 2.<class 'bs4.element.Tag ...

  6. Python3+迭代器与生成器

    转载Python3 迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素 ...

  7. Python3.x与Python2.x的差异用法

    Python3.x与Python2.x的差异用法 1,关于urllib2区别: # python2 import urllib2 # python3 # 用urllib.request代替urllib ...

  8. v0lt CTF安全工具包

    0×00 v0lt v0lt是一个我尝试重组每一个我使用过的/现在在使用的/将来要用的用python开发的安全领域CTF工具.实践任务可能会采用bash脚本来解决,但我认为Python更具有灵活性,这 ...

  9. 仿9GAG制作过程(二)

    有话要说: 这次准备讲述用python爬虫以及将爬来的数据存到MySQL数据库的过程,爬的是煎蛋网的无聊图. 成果: 准备: 下载了python3.7并配置好了环境变量 下载了PyCharm作为开发p ...

随机推荐

  1. 使用update可以防止并发问题(保证数据的准确性),如果使用select会产生并发问题 ; select * from xx for update 给查询开启事务,默认情况下是没有事物的

    update可以锁住数据防止数据被更新且导致与查询出的数据有误差,如果响应条数为0.说明更新失败 则可以回滚事务;

  2. codeforces740B

    Alyona and flowers CodeForces - 740B Little Alyona is celebrating Happy Birthday! Her mother has an ...

  3. codeforces509B

    Painting Pebbles CodeForces - 509B There are n piles of pebbles on the table, the i-th pile contains ...

  4. 【Tensorflow】Tensorflow入门教程

    基本使用 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...

  5. C语言实现字符串逆序输出

    方法一: #include <stdio.h> #include <stdlib.h> #include <string.h> void Reverse(char ...

  6. 解决Maven依赖报错问题

    问题现象:从svn上签下源代码,本地执行打包命令,提示如下错误: [ERROR] Failed to execute goal on project dddwriter: Could not reso ...

  7. Mysql 千万级别数据数据查询

    1.构建数据 --创建MyISAM模式表方便批量跑数据 CREATE TABLE `logs1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `logtype` v ...

  8. Python里的单下划线,双下划线,以及前后都带下划线的意义

    Python里的单下划线,双下划线,以及前后都带下划线的意义: 单下划线如:_name 意思是:不能通过from modules import * 导入,如需导入需要:from modules imp ...

  9. 【BZOJ3456】轩辕朗的城市规划 无向连通图计数 CDQ分治 FFT 多项式求逆 多项式ln

    题解 分治FFT 设\(f_i\)为\(i\)个点组成的无向图个数,\(g_i\)为\(i\)个点组成的无向连通图个数 经过简单的推导(枚举\(1\)所在的连通块大小),有: \[ f_i=2^{\f ...

  10. 运行os.fork()报AttributeError: module 'os' has no attribute 'fork'

    现象 报错代码 def handle(s, c, db): pid = os.fork() if pid == 0: s.close() do_child(c, db) sys.exit() else ...