Hướng dẫn beautifulsoup python install - cài đặt python beautifulsoup

Beautiful Soup 4

Mục đích: Parse HTML, XML và Website scraping: Parse HTML, XML và Website scraping: Parse HTML, XML và Website scraping

Bạn muốn parse HTML, XML hay đơn giản là muốn scraping dữ liệu từ một website nào đó với ngôn ngữ Python? Trên Viblo cũng có một số bài của Anh Tranngoc về Scraping và Crawling dữ liệu từ một website nào đó với module Scrapy như: Kỹ thuật scraping và crawling Web nâng cao với Scrapy và SQLAlchemy hay Scraping và crawling Web với Scrapy và SQLAlchemy. Nay mình cũng xin phép giới thiệu một module cũng có thể scraping dữ liệu giống Scrapy nhưng nó nhẹ và đơn giản hơn cho những yêu cầu không đòi hỏi sự phức tạp như Scrapy, đó là Beautiful Soup. Chúng ta cùng đi tìm hiểu nó nhé!Anh Tranngoc về Scraping và Crawling dữ liệu từ một website nào đó với module Scrapy như: Kỹ thuật scraping và crawling Web nâng cao với Scrapy và SQLAlchemy hay Scraping và crawling Web với Scrapy và SQLAlchemy. Nay mình cũng xin phép giới thiệu một module cũng có thể scraping dữ liệu giống Scrapy nhưng nó nhẹ và đơn giản hơn cho những yêu cầu không đòi hỏi sự phức tạp như Scrapy, đó là Beautiful Soup. Chúng ta cùng đi tìm hiểu nó nhé!Anh Tranngoc về Scraping và Crawling dữ liệu từ một website nào đó với module Scrapy như: Kỹ thuật scraping và crawling Web nâng cao với Scrapy và SQLAlchemy hay Scraping và crawling Web với Scrapy và SQLAlchemy. Nay mình cũng xin phép giới thiệu một module cũng có thể scraping dữ liệu giống Scrapy nhưng nó nhẹ và đơn giản hơn cho những yêu cầu không đòi hỏi sự phức tạp như Scrapy, đó là Beautiful Soup. Chúng ta cùng đi tìm hiểu nó nhé!

Cài đặt

Trước tiên, chúng ta có thể kiểm tra xem trên máy của mình đã cài module này chưa hoặc nếu đã cài rồi, chúng ta xem mình đã ở phiên bản mới nhất chưa bằng 1 đoạn code ngắn trên terminal như sau:

python -c "import bs4; print(bs4.__version__);"

Sau khi thử đoạn code trên, nếu máy bạn đã cài rồi thì nó sẽ xuất hiện phiên bản của module BeautifulSoup. Còn không, bạn sẽ nhận được một lỗi như sau:

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4

Để cài Beautiful Soup 4, bạn có bốn cách là thông qua APT, PIP, EasyInstall hoặc thông qua source. Mình sẽ đưa ra cả bốn cách cài để bạn có thể lựa chọn cho phù hợp với sở thích của mình nhé

Hướng dẫn beautifulsoup python install - cài đặt python beautifulsoup

  • Qua APT:
    Traceback (most recent call last):
      File "", line 1, in <module>
    ImportError: No module named bs4
    
    5
  • Qua PIP:
    Traceback (most recent call last):
      File "", line 1, in <module>
    ImportError: No module named bs4
    
    6
  • Qua EasyInstall:
    Traceback (most recent call last):
      File "", line 1, in <module>
    ImportError: No module named bs4
    
    7
  • Qua source:
    • Vào trang download của Beautiful Soup tại đây rồi download phiên bản mà bạn muốn sử dụng.tại đây rồi download phiên bản mà bạn muốn sử dụng.tại đây rồi download phiên bản mà bạn muốn sử dụng.
    • Xả nén source bằng lệnh:
      Traceback (most recent call last):
        File "", line 1, in <module>
      ImportError: No module named bs4
      
      8
    • Thay đổi thư mục hiện thời bằng lệnh:
      Traceback (most recent call last):
        File "", line 1, in <module>
      ImportError: No module named bs4
      
      9
    • Cài đặt:
      DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Documenttitle>
          <link rel="stylesheet" href="css.css" type="text/css">
      head>
      <body>
          
          <div class="items-list">
              <div class="item pull-right">
                  <p class="title">Item 001p>
                  <p class="price">Price: 01$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 002p>
                  <p class="price">Price: 02$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 003p>
                  <p class="price">Price: 03$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 004p>
                  <p class="price">Price: 04$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 005p>
                  <p class="price">Price: 05$p>
                  <p><a href="#">Buya>p>
              div>
          div>
          
      body>
      html>
      
      0

Nếu tất cả cách trên đều không thể cài đặt được, thì bạn vẫn có thể sử dụng nó bằng cách copy thư mục

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Documenttitle>
    <link rel="stylesheet" href="css.css" type="text/css">
head>
<body>
    
    <div class="items-list">
        <div class="item pull-right">
            <p class="title">Item 001p>
            <p class="price">Price: 01$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 002p>
            <p class="price">Price: 02$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 003p>
            <p class="price">Price: 03$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 004p>
            <p class="price">Price: 04$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 005p>
            <p class="price">Price: 05$p>
            <p><a href="#">Buya>p>
        div>
    div>
    
body>
html>
1 ở phần cài đặt bằng source vào thư mục source code của bạn rồi sử dụng như bình thường !

!

Cài đặt parser

Beautiful Soup hỗ trợ thư viện HTML parser mặc định của Python và một số thư viện của bên thứ ba. Ví dụ như lxml parse hoặc html5lib parser. Mình sẽ hướng dẫn các bạn cài đặt cả 2 thư viện này nhé !

!

HTML5Lib

Cài đặt parser

  • Beautiful Soup hỗ trợ thư viện HTML parser mặc định của Python và một số thư viện của bên thứ ba. Ví dụ như lxml parse hoặc html5lib parser. Mình sẽ hướng dẫn các bạn cài đặt cả 2 thư viện này nhé !
    • DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Documenttitle>
          <link rel="stylesheet" href="css.css" type="text/css">
      head>
      <body>
          
          <div class="items-list">
              <div class="item pull-right">
                  <p class="title">Item 001p>
                  <p class="price">Price: 01$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 002p>
                  <p class="price">Price: 02$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 003p>
                  <p class="price">Price: 03$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 004p>
                  <p class="price">Price: 04$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 005p>
                  <p class="price">Price: 05$p>
                  <p><a href="#">Buya>p>
              div>
          div>
          
      body>
      html>
      
      2
  • Để cài đặt HTML5Lib, bạn có thể làm theo 1 trong 3 cách:
    • DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Documenttitle>
          <link rel="stylesheet" href="css.css" type="text/css">
      head>
      <body>
          
          <div class="items-list">
              <div class="item pull-right">
                  <p class="title">Item 001p>
                  <p class="price">Price: 01$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 002p>
                  <p class="price">Price: 02$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 003p>
                  <p class="price">Price: 03$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 004p>
                  <p class="price">Price: 04$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 005p>
                  <p class="price">Price: 05$p>
                  <p><a href="#">Buya>p>
              div>
          div>
          
      body>
      html>
      
      3
  • Thông qua APT:
    • DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Documenttitle>
          <link rel="stylesheet" href="css.css" type="text/css">
      head>
      <body>
          
          <div class="items-list">
              <div class="item pull-right">
                  <p class="title">Item 001p>
                  <p class="price">Price: 01$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 002p>
                  <p class="price">Price: 02$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 003p>
                  <p class="price">Price: 03$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 004p>
                  <p class="price">Price: 04$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 005p>
                  <p class="price">Price: 05$p>
                  <p><a href="#">Buya>p>
              div>
          div>
          
      body>
      html>
      
      4

Thông qua PIP:

Thông qua EasyInstall

LXML
  • Để có thể cài đặt được LXML parser, bạn cần phải có 3 package sau:
    • DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Documenttitle>
          <link rel="stylesheet" href="css.css" type="text/css">
      head>
      <body>
          
          <div class="items-list">
              <div class="item pull-right">
                  <p class="title">Item 001p>
                  <p class="price">Price: 01$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 002p>
                  <p class="price">Price: 02$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 003p>
                  <p class="price">Price: 03$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 004p>
                  <p class="price">Price: 04$p>
                  <p><a href="#">Buya>p>
              div>
              <div class="item pull-right">
                  <p class="title">Item 005p>
                  <p class="price">Price: 05$p>
                  <p><a href="#">Buya>p>
              div>
          div>
          
      body>
      html>
      
      8
  • DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Documenttitle>
        <link rel="stylesheet" href="css.css" type="text/css">
    head>
    <body>
        
        <div class="items-list">
            <div class="item pull-right">
                <p class="title">Item 001p>
                <p class="price">Price: 01$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 002p>
                <p class="price">Price: 02$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 003p>
                <p class="price">Price: 03$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 004p>
                <p class="price">Price: 04$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 005p>
                <p class="price">Price: 05$p>
                <p><a href="#">Buya>p>
            div>
        div>
        
    body>
    html>
    
    5,
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Documenttitle>
        <link rel="stylesheet" href="css.css" type="text/css">
    head>
    <body>
        
        <div class="items-list">
            <div class="item pull-right">
                <p class="title">Item 001p>
                <p class="price">Price: 01$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 002p>
                <p class="price">Price: 02$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 003p>
                <p class="price">Price: 03$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 004p>
                <p class="price">Price: 04$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 005p>
                <p class="price">Price: 05$p>
                <p><a href="#">Buya>p>
            div>
        div>
        
    body>
    html>
    
    6 và
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Documenttitle>
        <link rel="stylesheet" href="css.css" type="text/css">
    head>
    <body>
        
        <div class="items-list">
            <div class="item pull-right">
                <p class="title">Item 001p>
                <p class="price">Price: 01$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 002p>
                <p class="price">Price: 02$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 003p>
                <p class="price">Price: 03$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 004p>
                <p class="price">Price: 04$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 005p>
                <p class="price">Price: 05$p>
                <p><a href="#">Buya>p>
            div>
        div>
        
    body>
    html>
    
    7.
    • Beautiful Soup hỗ trợ thư viện HTML parser mặc định của Python và một số thư viện của bên thứ ba. Ví dụ như lxml parse hoặc html5lib parser. Mình sẽ hướng dẫn các bạn cài đặt cả 2 thư viện này nhé !
      • DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Documenttitle>
            <link rel="stylesheet" href="css.css" type="text/css">
        head>
        <body>
            
            <div class="items-list">
                <div class="item pull-right">
                    <p class="title">Item 001p>
                    <p class="price">Price: 01$p>
                    <p><a href="#">Buya>p>
                div>
                <div class="item pull-right">
                    <p class="title">Item 002p>
                    <p class="price">Price: 02$p>
                    <p><a href="#">Buya>p>
                div>
                <div class="item pull-right">
                    <p class="title">Item 003p>
                    <p class="price">Price: 03$p>
                    <p><a href="#">Buya>p>
                div>
                <div class="item pull-right">
                    <p class="title">Item 004p>
                    <p class="price">Price: 04$p>
                    <p><a href="#">Buya>p>
                div>
                <div class="item pull-right">
                    <p class="title">Item 005p>
                    <p class="price">Price: 05$p>
                    <p><a href="#">Buya>p>
                div>
            div>
            
        body>
        html>
        
        9
    • Để cài đặt HTML5Lib, bạn có thể làm theo 1 trong 3 cách:
      • Traceback (most recent call last):
          File "", line 1, in <module>
        ImportError: No module named bs4
        
        70
    • Thông qua APT:
      • Traceback (most recent call last):
          File "", line 1, in <module>
        ImportError: No module named bs4
        
        71

Thông qua PIP:

  • Thông qua EasyInstall
    • Traceback (most recent call last):
        File "", line 1, in <module>
      ImportError: No module named bs4
      
      72
  • Để có thể cài đặt được LXML parser, bạn cần phải có 3 package sau:
    • Traceback (most recent call last):
        File "", line 1, in <module>
      ImportError: No module named bs4
      
      73
  • DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Documenttitle>
        <link rel="stylesheet" href="css.css" type="text/css">
    head>
    <body>
        
        <div class="items-list">
            <div class="item pull-right">
                <p class="title">Item 001p>
                <p class="price">Price: 01$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 002p>
                <p class="price">Price: 02$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 003p>
                <p class="price">Price: 03$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 004p>
                <p class="price">Price: 04$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 005p>
                <p class="price">Price: 05$p>
                <p><a href="#">Buya>p>
            div>
        div>
        
    body>
    html>
    
    5,
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Documenttitle>
        <link rel="stylesheet" href="css.css" type="text/css">
    head>
    <body>
        
        <div class="items-list">
            <div class="item pull-right">
                <p class="title">Item 001p>
                <p class="price">Price: 01$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 002p>
                <p class="price">Price: 02$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 003p>
                <p class="price">Price: 03$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 004p>
                <p class="price">Price: 04$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 005p>
                <p class="price">Price: 05$p>
                <p><a href="#">Buya>p>
            div>
        div>
        
    body>
    html>
    
    6 và
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Documenttitle>
        <link rel="stylesheet" href="css.css" type="text/css">
    head>
    <body>
        
        <div class="items-list">
            <div class="item pull-right">
                <p class="title">Item 001p>
                <p class="price">Price: 01$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 002p>
                <p class="price">Price: 02$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 003p>
                <p class="price">Price: 03$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 004p>
                <p class="price">Price: 04$p>
                <p><a href="#">Buya>p>
            div>
            <div class="item pull-right">
                <p class="title">Item 005p>
                <p class="price">Price: 05$p>
                <p><a href="#">Buya>p>
            div>
        div>
        
    body>
    html>
    
    7.
    • Cài đặt các required packages:
  • HTML5Lib
    • Traceback (most recent call last):
        File "", line 1, in <module>
      ImportError: No module named bs4
      
      76

Cài đặt LXML parser:

Để có thể cài đặt được LXML parser, bạn cần phải có 3 package sau:

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Documenttitle>
    <link rel="stylesheet" href="css.css" type="text/css">
head>
<body>
    
    <div class="items-list">
        <div class="item pull-right">
            <p class="title">Item 001p>
            <p class="price">Price: 01$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 002p>
            <p class="price">Price: 02$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 003p>
            <p class="price">Price: 03$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 004p>
            <p class="price">Price: 04$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 005p>
            <p class="price">Price: 05$p>
            <p><a href="#">Buya>p>
        div>
    div>
    
body>
html>
5,
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Documenttitle>
    <link rel="stylesheet" href="css.css" type="text/css">
head>
<body>
    
    <div class="items-list">
        <div class="item pull-right">
            <p class="title">Item 001p>
            <p class="price">Price: 01$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 002p>
            <p class="price">Price: 02$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 003p>
            <p class="price">Price: 03$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 004p>
            <p class="price">Price: 04$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 005p>
            <p class="price">Price: 05$p>
            <p><a href="#">Buya>p>
        div>
    div>
    
body>
html>
6 và
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Documenttitle>
    <link rel="stylesheet" href="css.css" type="text/css">
head>
<body>
    
    <div class="items-list">
        <div class="item pull-right">
            <p class="title">Item 001p>
            <p class="price">Price: 01$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 002p>
            <p class="price">Price: 02$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 003p>
            <p class="price">Price: 03$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 004p>
            <p class="price">Price: 04$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 005p>
            <p class="price">Price: 05$p>
            <p><a href="#">Buya>p>
        div>
    div>
    
body>
html>
7.

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Documenttitle>
    <link rel="stylesheet" href="css.css" type="text/css">
head>
<body>
    
    <div class="items-list">
        <div class="item pull-right">
            <p class="title">Item 001p>
            <p class="price">Price: 01$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 002p>
            <p class="price">Price: 02$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 003p>
            <p class="price">Price: 03$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 004p>
            <p class="price">Price: 04$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 005p>
            <p class="price">Price: 05$p>
            <p><a href="#">Buya>p>
        div>
    div>
    
body>
html>

Thông qua EasyInstall:

Cách sử dụng các parser

Python html.parser

LXML HTML parser

LXML XLM parserpdb (Python Debugger) để tiện cho việc debug code của chúng ta trên terminal. Nếu có thể, mình sẽ giới thiệu module pdb này trong bài viết tiếp theo của series PyMOTM này

). Để parser một tài liệu, bạn chỉ cần import
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
79 từ thư viện
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Documenttitle>
    <link rel="stylesheet" href="css.css" type="text/css">
head>
<body>
    
    <div class="items-list">
        <div class="item pull-right">
            <p class="title">Item 001p>
            <p class="price">Price: 01$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 002p>
            <p class="price">Price: 02$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 003p>
            <p class="price">Price: 03$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 004p>
            <p class="price">Price: 04$p>
            <p><a href="#">Buya>p>
        div>
        <div class="item pull-right">
            <p class="title">Item 005p>
            <p class="price">Price: 05$p>
            <p><a href="#">Buya>p>
        div>
    div>
    
body>
html>
1 rồi truyền file handle hoặc một chuỗi HTML (XML) vào constructor của
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
79 là có thể sử dụng được ngay. Xem ra món này nấu khá đơn giản đấy nhỉ

!
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
2

?

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
7
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
74 hoặc
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
75

Chuẩn bị nguyên liệupdb (Python Debugger) để tiện cho việc debug code của chúng ta trên terminal. Nếu có thể, mình sẽ giới thiệu module pdb này trong bài viết tiếp theo của series PyMOTM này

! Còn việc sử dụng parser nào thì tùy bạn quyết định nhé. Mình sẽ dùng HTML5Lib

Để thử Beautiful Soup, chúng ta sẽ tạo một file HTML với tên

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
77 trong thư mục
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
78 với nội dung như sau:

!

Cài đặt parser

Beautiful Soup hỗ trợ thư viện HTML parser mặc định của Python và một số thư viện của bên thứ ba. Ví dụ như lxml parse hoặc html5lib parser. Mình sẽ hướng dẫn các bạn cài đặt cả 2 thư viện này nhé !

Để cài đặt HTML5Lib, bạn có thể làm theo 1 trong 3 cách:

Let's go (gogo)!

Các loại object của Beautiful Soup

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
1

Beautiful Soup sẽ chuyển đổi tài liệu HTML sang Python tree object. Nhưng bạn chỉ cần quan tâm 4 loại object là:

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
24,
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
25,
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
79 và
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
27 thôi nhé !

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
6

Tag

Là một HTML (XML) tag

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
7

Name

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
8

Tên của tag. Bạn có thể lấy ra bằng cách sử dụng

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
28
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
0

Bạn cũng có thể thay đổi tên của tag bằng cách gán cho nó 1 cái tên

Attributes

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
1

Bạn cũng có thể thêm/sửa/xóa một giá trị trong danh sách các giá trị của attribute đó:

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
2

NavigableString

Là nội dung text của tag đó.

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
25 giống như một Python Unicode string, ngoại trừ việc nó hỗ trợ bạn một số tính năng như duyệt và tìm kiếm (sẽ được giới thiệu ở bài tiếp theo). Bạn cũng có thể convert nó sang kiểu Unicode string bằng function
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
07
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
3

Bạn không thể sửa nội dung văn bản của 1 tag bằng cách trực tiếp, nhưng có thể thay thế nó thông qua function

Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
08:
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named bs4
4

Bài viết cũng đã khá dài rồi mà chúng ta mới chỉ đi chưa được một nửa của Beautiful Soup, nên mình xin phép tạm dừng tại đây nhé. Phần 2 mình sẽ giới thiệu về việc duyệt và tìm kiếm nhé !

!