介绍:将一个html文档转换成BeautifulSoup对象,然后通过对象的方法或属性查找指定的节点内容

  • 转换本地文件:

​ soup = BeautifulSoup(fp,'lxml') fp为文档对象

  • 转换网络文件:

    soup = BeautifulSoup('str/bytes','lxml') 'str/bytes'通常为requests请求方法实例化对象的text或content属性

获取指定内容的方式

1.通过标签(soup.tagName)

1.1 根据标签查找

soup.tagName 查找到第一个符合要求的标签

例如:soup.a 查找到第一个a标签

1.2 获取属性

soup.tagName.attrs 获取标签的所有属性和属性值,返回一个字典

例如:soup.a.attrs 获取到第一个a标签 所有属性和属性值的一格字典

soup.tagName.attrs['key'] 获取上述字典的一个属性值,

通常使用soup.tagName[attr] 简化上面两个方法,例如:soup.a['href'],获取第一个a标签的href属性

1.3 获取内容

soup.a.string 获取a标签的文本内容,如果里面嵌套标签,则为None

``soup.a.text` 获取a下的所有文本(嵌套标签的文本)

``soup.a.get_text()`

2.find与find_all方法

2.1find(‘tagName',attr)

查找到第一个符合attr的‘tagName'标签

soup.find('a') 和soup.a相同, 查找到第一个a标签

soup.find('a', title = 'xxx') 查找第一个title为xxx的标签

soup.find('a',class_ = 'xxx') 注意使用类名时,避免关键字class,这里为class_

soup.find('a',id = 'xxx')

find之后还可以使用string、text和get_text() 例如:soup.find('a',class = 'xxx').string

同样也可以获取属性,例如:soup.find('a',id = 'xxx')['href']

2.2find_all

返回的是列表

soup.find_all('a') 返回一个含所有a标签的列表,要进一步获取某一个a的属性或内容,先从列表中获 取该元素

soup.find_all(['a','li']) 返回一个含所有a标签和li标签的列表

soup.find_all('a',limit = 3) 限定前三个a标签 (和python索引不同)

soup.find_all('div',class_="xxx") 返回所有类名为"xxx"的div标签

3.select()方法

select() 括号中和前端中的选择器(标签、类、id....)类似,select返回的也是列表

select('#id') select('.class') select('ul li')

select('div > span > a')

bs4解析练习

import requests
from bs4 import BeautifulSoup
url = 'https://www.coolapk.com/'
response = requests.get(url = url)
ht = response.text
soup = BeautifulSoup(ht,'lxml')
soup.a

<a href="/">
<span id="header-logo" style="display: flex;justify-content: left;align-items: center;"><img alt="" src="/static/images/header-logo.png" style="height: 35px;margin-right: 10px;"/><span>酷安</span></span>
</a>

soup.a.attrs   #{'href': '/'}
soup.a['herf']  # /
soup.a.string   #None
soup.a.text    #酷安
souo.a.get_text() #酷安

soup.find('li',id="navbar-apk")       #<li id="navbar-apk"><a href="/apk/">应用</a></li>
soup.find('li',id="navbar-apk").string #应用
soup.find('img',class_='logo')['src'] #/static/images/coolapklogo.png
soup.find_all('div',class_="weui-flex",limit = 2)

#结果
[<div class="weui-flex logo-box">
<div class="weui-flex__item">
<img alt="酷安" class="logo" src="/static/images/coolapklogo.png"/>
</div>
</div>, <div class="weui-flex">
<div class="weui-flex__item">
<p class="title">全新酷安V9</p>
<p class="sub-title mobile-display">和你一起,发现科技新生活</p>
<p class="sub-title pc-display">和你一起,<br/>发现科技新生活</p>
</div>
</div>]

div_L = soup.find_all('li',limit = 6)
for i in div_L:
print(i.text)
#结果
首页
应用
游戏
酷安手机APP
联系酷安
关于酷安

soup.select('.footer-navbar ul li a')
for i in tag_a_L:
print(i['href'])
#结果:
/about/about.html
/about/contact.html
/about/jobs.html
/about/copyright.html
/apk/com.coolapk.market?from=footer
https://developer.coolapk.com?from=footer

bs4解析的更多相关文章

  1. bs4解析库

    beautifulsoup4 bs4解析库是灵活又方便的网页解析库,处理高效,支持多种解析器.利用它不用编写正则表达式即可方便地实现网页的提取 要解析的html标签 from bs4 import B ...

  2. bs4 解析 以及用法

    bs4解析 bs4: 环境安装: lxml bs4 bs4编码流程: 1.实例化一个bs4对象,且将页面源码数据加载到该对象中 2.bs相关的方法或者属性实现标签定位 3.取文本或者取属性 bs的属性 ...

  3. Python3.x:bs4解析html基础用法

    Python3.x:bs4解析html基础用法 代码: import urllib.request from bs4 import BeautifulSoup import re url = r'ht ...

  4. 爬虫的三种解析方式(正则解析, xpath解析, bs4解析)

    一 : 正则解析 : 常用正则回顾: 单字符: . : 除换行符以外的所有字符 [] : [aoe] [a-w] 匹配集合中任意一个字符 \d : 数字 [0-9] \D : 非数字 \w : 非数字 ...

  5. python bs4解析网页时 bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to inst(转)

    Python小白,学习时候用到bs4解析网站,报错 bs4.FeatureNotFound: Couldn't find a tree builder with the features you re ...

  6. 爬虫系列二(数据清洗--->bs4解析数据)

    一 BeautifulSoup解析 1 环境安装 - 需要将pip源设置为国内源,阿里源.豆瓣源.网易源等 - windows (1)打开文件资源管理器(文件夹地址栏中) (2)地址栏上面输入 %ap ...

  7. pytho爬虫使用bs4 解析页面和提取数据

    页面解析和数据提取 关注公众号"轻松学编程"了解更多. 一般来讲对我们而言,需要抓取的是某个网站或者某个应用的内容,提取有用的价值.内容一般分为两部分,非结构化的数据 和 结构化的 ...

  8. bs4解析要获取被注掉的部分需先将注释符号去掉

    <div class="xzcf-content"> <div id="sfxz"> <div class="main- ...

  9. 爬虫的两种解析方式 xpath和bs4

    1.xpath解析 from lxml import etree 两种方式使用:将html文档变成一个对象,然后调用对象的方法去查找指定的节点 (1)本地文件 tree = etree.parse(文 ...

随机推荐

  1. 牛客OI周赛10-提高组:B-Taeyeon的困惑(值域线段树)

    做法 单点加单点删,在值域线段树上直接二分就能求值前\(K\)小的和 Code #include<bits/stdc++.h> typedef long long LL; const LL ...

  2. CSS Pixel 和 Device pixels

    Web developers need CSS pixels, that is, the pixels that are used in CSS declarations such as " ...

  3. elasticsearch配置jdk

    编辑bin/elasticsearch 可以看到elasticsearch使用环境变量JAVA_HOME中配置的jdk:if [ -x "$JAVA_HOME/bin/java" ...

  4. uiautomator代码例子--java

    在androidtest下创建文件Ui2Test.java package com.example.myapplication; import android.app.Instrumentation; ...

  5. fcntl 函数

    设置文件的flags,阻塞设置成非阻塞,非阻塞设置成阻塞(这连个在server开发中可以封装为基本函数) 线程引入 pthread_self 和 pthread_equal 原因 ——解决不同平台的问 ...

  6. kvm网卡配置

    https://blog.51cto.com/quliren/2046001 https://blog.51cto.com/quliren/2045555 https://blog.csdn.net/ ...

  7. C++在线编程网站

    1.推荐 http://www.dooccn.com/cpp/ 2.https://wandbox.org/ 3.https://www.tutorialspoint.com/compile_cpp_ ...

  8. angular 中的[ngClass]、[ngStyle]

    <div style="text-align:center"> <h1> Welcome to {{ title }}! </h1> </ ...

  9. linux性能监控 -CPU、Memory、IO、Network等指标的讲解

    [操作系统-linux]linux性能监控 -CPU.Memory.IO.Network等指标的讲解(转) 一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,Syst ...

  10. sed替换 - 含斜杠(\)和Shell变量

    gen_image.bat中的内容如下:   FOTARomPacker.exe -i .\_ini\FOTARomPacker.ini -o .\_Output\a.bin @IF %ERRORLE ...