环境准备:

bs4安装方法:https://blog.csdn.net/Bibabu135766/article/details/81662981
requests安装方法:https://blog.csdn.net/douguangyao/article/details/77922973 https://pypi.org/project/requests/#files 卸载pip:python -m pip uninstall pip 安装pip:https://pypi.python.org/pypi/pip#downloads

  

bs4用法介绍:Beautiful Soup和 lxml 一样, 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据。

https://www.cnblogs.com/amou/p/9184614.html

https://beautifulsoup.readthedocs.io/zh_CN/latest/

#!/usr/bin/env python
# -*- coding:utf- -*-
from bs4 import BeautifulSoup html = '''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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>
'''
#创建Beautiful Soup 对象
soup = BeautifulSoup(html,'lxml')
print soup,"--------------------------------"
# #格式化输出soup对象的内容
# print soup.prettify() #四大对象种类 Tag、NavigableString、BeautifulSoup、Comment
#一、Tag通俗点讲就是 HTML 中的一个个标签
# print soup.html
print soup.p,'----p标签的内容'
print soup.p.attrs,'----打印p标签的属性'
print soup.p['class'],soup.p['name']
print soup.head
print soup.name,soup.head.name,'----打印标签名称'
#二、NavigableString 要想获取标签内部的文字怎么办呢?很简单,用 .string 即可
print soup.p.string,'----p标签内的文字'
print type(soup.p.string)
#三、BeautifulSoup 对象表示的是一个文档的内容。大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性
print soup.name
print type(soup.name)
print soup.attrs,'----文档本身的属性为空'
#四、Comment 对象是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号。
print soup.a
print soup.a.string
print type(soup.a.string),'----Comment是一种特殊的NavigableString 对象'

打印结果如下:

BeautifulSoup4查找、正则使用:

#!/usr/bin/env python
# -*- coding:utf- -*-
from bs4 import BeautifulSoup
import resoup = BeautifulSoup(html,'lxml')
#print soup,'------------html文档--------------' print soup.find_all('b'),'----find b 标签'
for tag in soup.find_all(re.compile('^b')):
print tag.name,'----re正则找出所有b开头的标签'
print soup.find_all(id='link1')
print soup.find_all(text='Tillie'),'----通过 text 参数可以搜搜文档中的字符串内容'
print soup.find_all(text=["Tillie",'Lacie'])
print soup.find_all(text=re.compile('Dormouse'))

打印结果如下:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

requests用法介绍:

http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

https://cuiqingcai.com/2556.html

python 爬虫之beautifulsoup(bs4)环境准备的更多相关文章

  1. 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解(新手必学)

    为大家介绍下Python爬虫库BeautifulSoup遍历文档树并对标签进行操作的详细方法与函数下面就是使用Python爬虫库BeautifulSoup对文档树进行遍历并对标签进行操作的实例,都是最 ...

  2. Python爬虫——用BeautifulSoup、python-docx爬取廖雪峰大大的教程为word文档

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 廖雪峰大大贡献的教程写的不错,写了个爬虫把教程保存为word文件,供大家方便下载学习:http://p ...

  3. 【Python爬虫】BeautifulSoup网页解析库

    BeautifulSoup 网页解析库 阅读目录 初识Beautiful Soup Beautiful Soup库的4种解析器 Beautiful Soup类的基本元素 基本使用 标签选择器 节点操作 ...

  4. Python爬虫之BeautifulSoup的用法

    之前看静觅博客,关于BeautifulSoup的用法不太熟练,所以趁机在网上搜索相关的视频,其中一个讲的还是挺清楚的:python爬虫小白入门之BeautifulSoup库,有空做了一下笔记: 一.爬 ...

  5. python爬虫入门--beautifulsoup

    1,beautifulsoup的中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 2, from bs4 import Be ...

  6. python爬虫之Beautifulsoup学习笔记

    相关内容: 什么是beautifulsoup bs4的使用 导入模块 选择使用解析器 使用标签名查找 使用find\find_all查找 使用select查找 首发时间:2018-03-02 00:1 ...

  7. Python爬虫系列-BeautifulSoup详解

    安装 pip3 install beautifulsoup4 解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(markup,'html,parser') Pyth ...

  8. Python爬虫实践~BeautifulSoup+urllib+Flask实现静态网页的爬取

    爬取的网站类型: 论坛类网站类型 涉及主要的第三方模块: BeautifulSoup:解析.遍历页面 urllib:处理URL请求 Flask:简易的WEB框架 介绍: 本次主要使用urllib获取网 ...

  9. Python爬虫之Beautifulsoup模块的使用

    一 Beautifulsoup模块介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Be ...

随机推荐

  1. python实例七

    https://www.cnblogs.com/evablogs/p/6791548.html 题目:将一个列表的数据复制到另一个列表中. 程序分析:打算利用for循环和append函数来复制到另一个 ...

  2. laravel部署创建新项目 助记

    打开进入homestead虚拟机,并进入code文件夹 cd ~/homestead && vagrant up && vagrant ssh cd ~/Code 进行 ...

  3. new 和 newInstance 的区别

    初始化一个类,生成一个实例的时候:newInstance() 和 new 有什么区别? 用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有两 ...

  4. LeetCode算法题-Employee Importance(Java实现)

    这是悦乐书的第291次更新,第309篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第159题(顺位题号是690).定义员工信息的数据结构,其中包括员工的唯一ID,他的重要 ...

  5. nysql报错1136

    报错信息:> 1136 - Column count doesn't match value count at row 1 代码:insert into class(caption) value ...

  6. python import详解

    1.import作用 引入模块 2.import的特点 一个程序中,import的模块不会重复被引用,如: # test1.py import test2 print test2.attr # tes ...

  7. php使用root用户启动

    一般情况下,肯定是不推荐使用root用户启动php的 但是在某些服务器管理想使用WEB的方式来控制操作的话,那么就必须要使用root用户才有权限操作 1.修改配置文件php-fpm.conf的启动用户 ...

  8. python之zip打包

    import zipfile # 压缩 z = zipfile.ZipFile('z.zip', 'w') z.write('xo.xml') z.write('xxxoo.xml') z.close ...

  9. codeforces#983 B. XOR-pyramid (dp)

    参考博客:https://www.01hai.com/note/av137952. 题意:首先定义 (b代表一个数组) 给出一个区间,l,r,求它最大的连续子序列的函数值 分析: 定义dp[x][y] ...

  10. [转帖]SAP BASIS日常需要做的工作

    SAP BASIS日常需要做的工作 https://www.cnblogs.com/swordxia/p/4790684.html SAP Basis的一些日常工作包括用户权限管理.集团管理.数据库管 ...