bs 4の使い方

21086 ワード

ここに目次の見出しを書きます

  • BS4
  • 取付
  • 対象種別
  • BS4

  • Beautiful Soupは、HTMLまたはXMLファイルからデータを抽出できるPythonライブラリである.それはあなたの好きな変換器を通じて慣用的なドキュメントのナビゲーションを実現することができて、ドキュメントを探して、修正する方法.Beautiful Soupは、数時間から数日の作業時間を節約します.

  • インストール

  • pip install beautifulsoup4
  • 解析器
  • 式#シキ#
    使用方法
    メリット
    Python標準ライブラリ
    BeautifulSoup(markup, “html.parser”)
    Pythonの内蔵標準ライブラリは、ドキュメントのフォールトトレランスに適した速度で実行できます.
    lxml HTML解析器
    BeautifulSoup(markup, “lxml”)
    高速ドキュメントのフォールトトレランス機能
    lxml XML解析器
    BeautifulSoup(markup, [“lxml-xml”]) BeautifulSoup(markup, “xml”)
    速度が速くXMLを唯一サポートする解析器
    html5lib
    BeautifulSoup(markup, “html5lib”)
    最適なフォールトトレランスは、ドキュメントをブラウザで解析してHTML 5形式のドキュメントを生成します.

    対象の種類

  • Tag
    soup = BeautifulSoup('Extremely bold')
    tag = soup.b
    type(tag)
    # 
    
  • Name
    tag.name
    # 'b'
    
  • attrs
    tag.attrs
    # {u'class': u'boldest'}
    
  • NavigableString
    tag.string
    #Extremely bold
    
  • ドキュメントツリー
    html_doc = """
    <html><head><title>The Dormouse's storytitle>head>
    <body>
    <p class="title"><b>The Dormouse's storyb>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">Elsiea>,
    <a href="http://example.com/lacie" class="sister" id="link2">Laciea> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tilliea>;
    and they lived at the bottom of a well.p>
    
    <p class="story">...p>
    """
    
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    の検索
  • find_all(name, attrs, recursive, text, **kwargs)
  • 文字列
    soup.find_all('b')
    # [The Dormouse's story]
    
  • 正則
    import re
    for tag in soup.find_all(re.compile("^b")):
        print(tag.name)
    # body
    # b
    
  • リスト
    soup.find_all(["a", "b"])
    # [The Dormouse's story,
    #  Elsie,
    #  Lacie,
    #  Tillie]
    
  • キーワード
    soup.find_all(id='link2')
    # [Lacie]
    soup.find_all(href=re.compile("elsie"))
    # [Elsie]
    
  • CSS検索
    soup.find_all("a", class_="sister")
    # [Elsie,
    #  Lacie,
    #  Tillie]
    

  • CSSセレクタ
    soup.select("title")
    # [The Dormouse's story]
    
    soup.select("p nth-of-type(3)")
    # [

    ...

    ]
  • tagタグを介して層毎に
    soup.select("body a")
    # [Elsie,
    #  Lacie,
    #  Tillie]
    
    soup.select("html head title")
    # [The Dormouse's story]
    
  • を検索する.
  • あるtagタグの下の直接サブタグ
    soup.select("head > title")
    # [The Dormouse's story]
    
    soup.select("p > a")
    # [Elsie,
    #  Lacie,
    #  Tillie]
    
    soup.select("p > a:nth-of-type(2)")
    # [Lacie]
    
    soup.select("p > #link1")
    # [Elsie]
    
    soup.select("body > a")
    # []
    
  • を見つける.
  • 兄弟ノードラベルが見つかりました:
    soup.select("#link1 ~ .sister")
    # [Lacie,
    #  Tillie]
    
    soup.select("#link1 + .sister")
    # [Lacie]
    
  • CSSのクラス名で検索
    soup.select(".sister")
    # [Elsie,
    #  Lacie,
    #  Tillie]
    
    soup.select("[class~=sister]")
    # [Elsie,
    #  Lacie,
    #  Tillie]
    
  • tagのid検索:
    soup.select("#link1")
    # [Elsie]
    
    soup.select("a#link2")
    # [Lacie]
    
  • 複数のCSSセレクタで要素を同時に問い合わせる:
    soup.select("#link1,#link2")
    # [Elsie,
    #  Lacie]
    
  • プロパティが存在するかどうかで検索:
    soup.select('a[href]')
    # [Elsie,
    #  Lacie,
    #  Tillie]
    
  • プロパティの値によって検索:
    soup.select('a[href="http://example.com/elsie"]')
    # [Elsie]
    
    soup.select('a[href^="http://example.com/"]')
    # [Elsie,
    #  Lacie,
    #  Tillie]
    
    soup.select('a[href$="tillie"]')
    # [Tillie]
    
    soup.select('a[href*=".com/el"]')
    # [Elsie]
    
  • は、検索された要素の最初の
    soup.select_one(".sister")
    # Elsie
    
  • を返す.