zoukankan      html  css  js  c++  java
  • Python爬虫小白入门(十)Python 爬虫 – BeautifulSoup分析页面

    我们已经抓取了一个HTML页面,接下来,我们使用BeautifulSoup来分析页面。

    import requests
    from bs4 import BeautifulSoup
    
    page = requests.get("https://kevinhwu.github.io/demo/python-scraping/simple.html")
    
    soup = BeautifulSoup(page.content, 'html.parser')

    导入BeautifulSoup库,创建页面解析对象soup

    前面打印出的html页面格式很乱,如果想打印出美化格式的html页面,可以使用BeautifulSoup对象上的prettify方法:

    print(soup.prettify())

    输出

    <!DOCTYPE html>
    <html>
     <head>
      <title>
       A simple example page
      </title>
     </head>
     <body>
      <p>
       Here is some simple content for this page.
      </p>
     </body>
    </html>

    html文档解析后,文档中的html元素构成一个树形结构。可以使用BeautifulSoup对象上的children属性(类型是list_iterator),访问页面的顶层元素。

    list(soup.children)

    输出

    ['html', '
    ', <html>
    <head>
    <title>
    A simple example page
    </title>
    </head>
    <body>
    <p>
    Here is some simple content for this page.
    </p>
    </body>
    </html>, '
    ']

    可以看到,页面顶层有2个元素:

    • <!DOCTYPE html>初始标签
    • <html>标签

    列表中还有2个换行符( )。可以看一下列表中元素的类型是什么:

    [type(item) for item in list(soup.children)]

    输出

    [<class 'bs4.element.Doctype'>, <class 'bs4.element.NavigableString'>, <class 'bs4.element.Tag'>, <class 'bs4.element.NavigableString'>]

    如上所示,所有对象都是BeautifulSoup中的对象:

    • bs4.element.Doctype – Doctype对象,包含关于文档类型的信息
    • bs4.element.Tag – Tag对象,表示html 标签,对象中会嵌套其他标签
    • bs4.element.NavigableString – 表示HTML文档中的文本,此处是指2个换行符文本的类型

    Tag对象是最重要的对象类型,是我们最常打交道的对象类型。Tag对象让我们可以遍历,提取HTML文档中的标签和文本。

    返回HTML文档顶层子节点的第3个节点,即<html>标签。

    html = list(soup.children)[2]

    返回的节点html也是一个BeautifulSoup对象,因此可以继续访问该节点的子节点:

    list(html.children)

    输出

    ['
    ', <head>
    <title>
    A simple example page
    </title>
    </head>, '
    ', <body>
    <p>
    Here is some simple content for this page.
    </p>
    </body>, '
    ']

    可以看到,忽略换行符,这里有2个标签,headbody

    尝试提取p标签中的文本,先找到body

    body = list(html.children)[3]

    获取body标签的子标签:

    list(body.children)

    输出

    ['
    ', <p>
    Here is some simple content for this page.
    </p>, '
    ']

    提取p标签:

    p = list(body.children)[1]

    得到p标签后,就可以使用get_text方法来提取标签内的文本:

    import requests
    from bs4 import BeautifulSoup
    page=requests.get("https://kevinhwu.github.io/demo/python-scraping/simple.html")
    soup=BeautifulSoup(page.content,'html.parser')
    # print(soup.prettify())
    html = list(soup.children)[2]
    body = list(html.children)[3]
    p = list(body.children)[1]
    print(p.get_text())

    输出

    C:Anaconda3python.exe "C:Program FilesJetBrainsPyCharm 2019.1.1helperspydevpydevconsole.py" --mode=client --port=54852
    import sys; print('Python %s on %s' % (sys.version, sys.platform))
    sys.path.extend(['C:\app\PycharmProjects', 'C:/app/PycharmProjects'])
    Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.
    PyDev console: using IPython 7.12.0
    Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32
    runfile('C:/app/PycharmProjects/ArtificialIntelligence/test2.py', wdir='C:/app/PycharmProjects/ArtificialIntelligence')
    Here is some simple content for this page.
  • 相关阅读:
    hbase 相关
    java 连接 hiveserver2 例子
    ik_max_word ik_smart 区别 和 单字 查询 不到问题
    如何计算地址线和数据线
    golang consistent hash 菜鸟分析
    借助GitHub托管你的项目代码
    PHP性能追踪及分析工具xhprof的安装与使用
    git使用方法
    go开发
    go的websocket实现
  • 原文地址:https://www.cnblogs.com/huanghanyu/p/13175591.html
Copyright © 2011-2022 走看看