环境准备:

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. Left Jion等价SQL猜想验证

    猜想:以下两条SQL等价 select * from A left join B on A.ID=B.BID and B.BName=N'小明' select * from A left join ( ...

  2. MySQL之Innodb恢复的学习笔记

    MySQL · 引擎特性 · InnoDB 崩溃恢复过程 enum { SRV_FORCE_IGNORE_CORRUPT = 1, /*!< let the server run even if ...

  3. Linux内存描述之内存页面page--Linux内存管理(四)

    1 Linux如何描述物理内存 Linux把物理内存划分为三个层次来管理 层次 描述 存储节点(Node) CPU被划分为多个节点(node), 内存则被分簇, 每个CPU对应一个本地物理内存, 即一 ...

  4. 连接到github

    1,创建秘钥 $ ssh-keygen -t rsa -C "youremail@example.com"执行成功后,会在~/.ssh/目录下生成id_rsa和id_rsa.pub ...

  5. Servlet是否单例?

    1,测试环境: Java SE版本:1.8.0_161(AMD64) Tomcat版本:9.0.7(AMD64) 2,试验 (1)编写HelloServlet. 由于测试代码很简单,此处只列出doGe ...

  6. Vim 宏

    宏的概念 什么是宏呢?英文名:macro,代表一串命令的集合. 示例操作文本 SELECT * FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`u ...

  7. IO多路复用三种方式select/poll/epoll

    select多并发socket例子: #_*_coding:utf-8_*_ __author__ = 'Alex Li' import select import socket import sys ...

  8. 第一节 anaconda+jupyter+numpy简单使用

    数据分析:是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出所研究对象的内在规律 数据分析三剑客:Numpy,Pandas,Matplotlib 一 Anaconda 1 下载 官网:http ...

  9. Python开发【第二篇】运算符

    "+" 加号 __author__ = 'Tang' a = 8 b = 9 c = a + b a = 8.0 b = 9 c = a + b print(c) # 17.0 a ...

  10. HBase2.0中的Benchmark工具 — PerformanceEvaluation

    简介 在项目开发过程中,我们经常需要一些benchmark工具来对系统进行压测,以获得系统的性能参数,极限吞吐等等指标. 而在HBase中,就自带了一个benchmark工具—PerformanceE ...