Cách xử lý các ký tự đặc biệt trong mysql

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
57

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
58

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
59

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
60

Đôi khi chúng ta cần đưa các ký tự đặc biệt vào một chuỗi ký tự và lúc đó chúng phải được thoát hoặc bảo vệ. Chúng ta cần theo đuổi một số quy tắc cơ bản để thoát khỏi các ký tự đặc biệt được đưa ra dưới đây -

Ký tự thoát [\] có thể được thoát thành [\]

Ví dụ

mysql> Select 'A\B';
+-----+
| A\B |
+-----+
| A\B |
+-----+
1 row in set [0.00 sec]

Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 61 bên trong câu lệnh

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
62 của mình như thế này

SELECT 'Hello, I'm Nathan';

Truy vấn trên sẽ kích hoạt ________ 763 vì bạn đang đặt ký hiệu trích dẫn ________ 761 được sử dụng làm dấu phân cách cho chuỗi

Để sửa lỗi, bạn cần thoát khỏi ký tự được hiểu là dấu phân cách của chuỗi

Trong MySQL, bạn có thể thoát các ký hiệu trích dẫn bằng cách xen kẽ giữa các ký hiệu trích dẫn đơn và kép

Nếu bạn đang sử dụng ký hiệu trích dẫn đơn trong chuỗi của mình, hãy sử dụng trích dẫn kép cho dấu phân cách chuỗi như minh họa bên dưới

Beautiful Soup là một thư viện Python để lấy dữ liệu ra khỏi các tệp HTML và XML. Nó hoạt động với trình phân tích cú pháp yêu thích của bạn để cung cấp các cách điều hướng, tìm kiếm và sửa đổi cây phân tích thành ngữ. Nó thường tiết kiệm cho lập trình viên hàng giờ hoặc ngày làm việc

Các hướng dẫn này minh họa tất cả các tính năng chính của Beautiful Soup 4, với các ví dụ. Tôi chỉ cho bạn thấy thư viện tốt cho việc gì, cách nó hoạt động, cách sử dụng nó, cách khiến nó làm những gì bạn muốn và phải làm gì khi nó vi phạm mong đợi của bạn

Tài liệu này bao gồm Beautiful Soup phiên bản 4. 11. 0. Các ví dụ trong tài liệu này được viết cho Python 3. 8

Có thể bạn đang tìm tài liệu về Beautiful Soup 3. Nếu vậy, bạn nên biết rằng Beautiful Soup 3 không còn được phát triển nữa và mọi hỗ trợ dành cho nó đã bị hủy bỏ vào ngày 31 tháng 12 năm 2020. Nếu bạn muốn tìm hiểu về sự khác biệt giữa Beautiful Soup 3 và Beautiful Soup 4, hãy xem

Tài liệu này đã được người dùng Beautiful Soup dịch sang các ngôn ngữ khác

  • 这篇文档当然还有中文版

  • このページは日本語で利用できます[外部リンク]

  • 이 문서는 한국어 번역도 가능합니다

  • Tài liệu này cũng được phát hành tại Bồ Đào Nha ở Brasil

  • Эта документация доступна на русском языке

Tìm sự giúp đỡ

Nếu bạn có thắc mắc về Beautiful Soup, hoặc gặp vấn đề,. Nếu vấn đề của bạn liên quan đến việc phân tích cú pháp một tài liệu HTML, hãy nhớ đề cập đến tài liệu đó

Bắt đầu nhanh

Đây là một tài liệu HTML mà tôi sẽ sử dụng làm ví dụ xuyên suốt tài liệu này. Đó là một phần của câu chuyện từ Alice in Wonderland

html_doc = """The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

"""

Chạy tài liệu “ba chị em” thông qua Beautiful Soup cho chúng ta một đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06, đại diện cho tài liệu dưới dạng cấu trúc dữ liệu lồng nhau

________số 8

Dưới đây là một số cách đơn giản để điều hướng cấu trúc dữ liệu đó

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie

One common task is extracting all the URLs found within a page’s Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 07 hoặc

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
08. Tên gói là
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
09. Đảm bảo rằng bạn sử dụng đúng phiên bản
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
08 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
07 cho phiên bản Python của mình [các phiên bản này có thể được đặt tên lần lượt là
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
12 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
13]

$ easy_install beautifulsoup4

$ pip cài đặt beautifulsoup4

[Gói

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 không phải là thứ bạn muốn. Đó là bản phát hành chính trước đó, Beautiful Soup 3. Rất nhiều phần mềm sử dụng BS3, vì vậy nó vẫn có sẵn, nhưng nếu bạn đang viết mã mới, bạn nên cài đặt
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
09. ]

Nếu bạn chưa cài đặt

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
07 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
08, bạn có thể tải xuống tarball nguồn Beautiful Soup 4 và cài đặt nó với
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
18

thiết lập $ trăn. cài đặt py

Nếu vẫn thất bại, giấy phép của Beautiful Soup cho phép bạn đóng gói toàn bộ thư viện với ứng dụng của mình. Bạn có thể tải xuống tarball, sao chép thư mục

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
19 của nó vào cơ sở mã của ứng dụng và sử dụng Beautiful Soup mà không cần cài đặt nó

Tôi sử dụng Python3. 8 để phát triển Beautiful Soup, nhưng nó sẽ hoạt động với các phiên bản gần đây khác

Cài đặt trình phân tích cú pháp

Beautiful Soup hỗ trợ trình phân tích cú pháp HTML có trong thư viện chuẩn của Python, nhưng nó cũng hỗ trợ một số trình phân tích cú pháp Python của bên thứ ba. Một là trình phân tích cú pháp lxml. Tùy thuộc vào thiết lập của bạn, bạn có thể cài đặt lxml bằng một trong các lệnh sau

$ apt-get cài đặt python-lxml

$ easy_install lxml

$ pip cài đặt lxml

Một giải pháp thay thế khác là trình phân tích cú pháp html5lib thuần Python, phân tích cú pháp HTML theo cách trình duyệt web thực hiện. Tùy thuộc vào thiết lập của bạn, bạn có thể cài đặt html5lib bằng một trong các lệnh sau

$ apt-get cài đặt python-html5lib

$ easy_install html5lib

$ pip cài đặt html5lib

Bảng này tóm tắt những ưu điểm và nhược điểm của từng thư viện trình phân tích cú pháp

Trình phân tích cú pháp

sử dụng điển hình

Thuận lợi

Nhược điểm

HTML của Python. trình phân tích cú pháp

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
20

  • bao gồm pin

  • Tốc độ khá

  • Khoan dung [Kể từ Python 3. 2]

  • Không nhanh bằng lxml, kém nhẹ nhàng hơn html5lib

trình phân tích cú pháp HTML của lxml

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
21

  • Rất nhanh

  • khoan dung

  • Phụ thuộc C bên ngoài

trình phân tích cú pháp XML của lxml

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
22
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
23

  • Rất nhanh

  • Trình phân tích cú pháp XML duy nhất hiện được hỗ trợ

  • Phụ thuộc C bên ngoài

html5lib

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
24

  • cực kỳ khoan dung

  • Phân tích các trang giống như cách trình duyệt web thực hiện

  • Tạo HTML5 hợp lệ

  • Rất chậm

  • Phụ thuộc Python bên ngoài

Nếu có thể, tôi khuyên bạn nên cài đặt và sử dụng lxml để tăng tốc. Nếu bạn đang sử dụng phiên bản Python rất cũ – sớm hơn 3. 2. 2 – bạn cần cài đặt lxml hoặc html5lib. Trình phân tích cú pháp HTML tích hợp của Python không tốt lắm trong các phiên bản cũ đó

Lưu ý rằng nếu một tài liệu không hợp lệ, các trình phân tích cú pháp khác nhau sẽ tạo ra các cây Súp đẹp khác nhau cho tài liệu đó. Xem để biết chi tiết

Nấu súp

Để phân tích một tài liệu, hãy chuyển nó vào hàm tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06. Bạn có thể truyền vào một chuỗi hoặc một xử lý tệp đang mở

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
1

Đầu tiên, tài liệu được chuyển đổi thành Unicode và các thực thể HTML được chuyển đổi thành các ký tự Unicode

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
2

Beautiful Soup sau đó phân tích cú pháp tài liệu bằng trình phân tích cú pháp tốt nhất hiện có. Nó sẽ sử dụng trình phân tích cú pháp HTML trừ khi bạn đặc biệt yêu cầu nó sử dụng trình phân tích cú pháp XML. [Nhìn thấy. ]

Các loại đối tượng

Beautiful Soup biến một tài liệu HTML phức tạp thành một cây các đối tượng Python phức tạp. Nhưng bạn sẽ chỉ phải xử lý khoảng bốn loại đối tượng.

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
29

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26

Đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 tương ứng với thẻ XML hoặc HTML trong tài liệu gốc

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
9

Các thẻ có rất nhiều thuộc tính và phương thức, và tôi sẽ đề cập đến hầu hết chúng trong và. Hiện tại, các tính năng quan trọng nhất của thẻ là tên và thuộc tính của nó

Tên

Mỗi thẻ có một tên, có thể truy cập dưới dạng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
32

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
1

Nếu bạn thay đổi tên của thẻ, thay đổi đó sẽ được phản ánh trong bất kỳ đánh dấu HTML nào do Beautiful Soup tạo ra

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
2

Thuộc tính

Một thẻ có thể có bất kỳ số thuộc tính nào. Thẻ

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
33 có thuộc tính “id” có giá trị là “đậm nhất”. Bạn có thể truy cập các thuộc tính của thẻ bằng cách coi thẻ như một cuốn từ điển

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
0

Bạn có thể truy cập trực tiếp từ điển đó dưới dạng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
34

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
1

Bạn có thể thêm, xóa và sửa đổi thuộc tính của thẻ. Một lần nữa, điều này được thực hiện bằng cách coi thẻ như một từ điển

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
2

Thuộc tính đa giá trị

HTML 4 định nghĩa một vài thuộc tính có thể có nhiều giá trị. HTML 5 loại bỏ một vài trong số chúng, nhưng định nghĩa thêm một số. Thuộc tính đa giá trị phổ biến nhất là

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
35 [nghĩa là một thẻ có thể có nhiều hơn một lớp CSS]. Những người khác bao gồm
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
36,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
37,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
38,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
39 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
40. Beautiful Soup trình bày [các] giá trị của thuộc tính đa giá trị dưới dạng danh sách

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
3

Nếu một thuộc tính có vẻ như có nhiều hơn một giá trị, nhưng đó không phải là thuộc tính đa giá trị như được xác định bởi bất kỳ phiên bản nào của tiêu chuẩn HTML, thì Beautiful Soup sẽ để nguyên thuộc tính đó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
4

Khi bạn biến thẻ trở lại thành chuỗi, nhiều giá trị thuộc tính được hợp nhất

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
5

Bạn có thể vô hiệu hóa điều này bằng cách chuyển

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
41 làm đối số từ khóa vào hàm tạo
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
6

Bạn có thể sử dụng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
43 để nhận giá trị luôn là danh sách, cho dù đó có phải là thuộc tính đa giá trị hay không

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
7

Nếu bạn phân tích một tài liệu dưới dạng XML, sẽ không có thuộc tính đa giá trị nào

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
8

Một lần nữa, bạn có thể định cấu hình điều này bằng cách sử dụng đối số

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
44

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
9

Có thể bạn sẽ không cần làm điều này, nhưng nếu có, hãy sử dụng các giá trị mặc định làm hướng dẫn. Họ thực hiện các quy tắc được mô tả trong đặc tả HTML

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
0

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27

Một chuỗi tương ứng với một đoạn văn bản trong thẻ. Beautiful Soup sử dụng lớp

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 để chứa các đoạn văn bản này

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
1

Một

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 giống như một chuỗi Python Unicode, ngoại trừ việc nó cũng hỗ trợ một số tính năng được mô tả trong và. Bạn có thể chuyển đổi một
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 thành một chuỗi Unicode với
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
49

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
2

Bạn không thể chỉnh sửa chuỗi tại chỗ, nhưng bạn có thể thay thế chuỗi này bằng chuỗi khác bằng cách sử dụng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
3

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 hỗ trợ hầu hết các tính năng được mô tả trong và , nhưng không phải tất cả chúng. Cụ thể, vì một chuỗi không được chứa bất kỳ thứ gì [theo cách một thẻ có thể chứa một chuỗi hoặc một thẻ khác], các chuỗi không hỗ trợ các thuộc tính
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
51 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52 hoặc phương thức
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53

Nếu bạn muốn sử dụng một

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 bên ngoài Beautiful Soup, bạn nên gọi
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
55 trên nó để biến nó thành một chuỗi Python Unicode bình thường. Nếu không, chuỗi của bạn sẽ mang một tham chiếu đến toàn bộ cây phân tích Beautiful Soup, ngay cả khi bạn đã sử dụng xong Beautiful Soup. Đây là một sự lãng phí bộ nhớ lớn

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06

Đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 đại diện cho toàn bộ tài liệu được phân tích cú pháp. Đối với hầu hết các mục đích, bạn có thể coi nó như một đối tượng. Điều này có nghĩa là nó hỗ trợ hầu hết các phương pháp được mô tả trong và

Bạn cũng có thể chuyển một đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 vào một trong các phương thức được định nghĩa trong , giống như cách bạn làm với một. Điều này cho phép bạn làm những việc như kết hợp hai tài liệu được phân tích cú pháp

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
4

Vì đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 không tương ứng với thẻ HTML hoặc XML thực nên nó không có tên và không có thuộc tính. Nhưng đôi khi nó rất hữu ích để xem
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
32 của nó, vì vậy nó đã được đặt tên là “[tài liệu]”
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
32 đặc biệt

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
5

Nhận xét và các chuỗi đặc biệt khác

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 bao gồm hầu hết mọi thứ bạn sẽ thấy trong tệp HTML hoặc XML, nhưng có một số bit còn sót lại. Cái chính mà bạn có thể gặp phải là bình luận

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
6

Đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
29 chỉ là một loại đặc biệt của
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
7

Nhưng khi nó xuất hiện như một phần của tài liệu HTML, một

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
29 được hiển thị với định dạng đặc biệt

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
8

Beautiful Soup cũng định nghĩa các lớp có tên là

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
68,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
69 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
70, dành cho biểu định kiểu CSS được nhúng [bất kỳ chuỗi nào được tìm thấy bên trong thẻ
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
71], Javascript được nhúng [bất kỳ chuỗi nào được tìm thấy trong thẻ
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
72] và mẫu HTML [bất kỳ chuỗi nào bên trong thẻ
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
73]. Các lớp này hoạt động giống hệt như
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27; . [Các lớp này mới trong Beautiful Soup 4. 9. 0 và trình phân tích cú pháp html5lib không sử dụng chúng. ]

Beautiful Soup định nghĩa các lớp cho bất kỳ thứ gì khác có thể hiển thị trong tài liệu XML.

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
75,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
76,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
77 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
78. Giống như
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
29, các lớp này là các lớp con của
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 bổ sung thêm thứ gì đó vào chuỗi. Đây là một ví dụ thay thế nhận xét bằng khối CDATA

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
9

Điều hướng cây

Đây lại là tài liệu HTML “Ba chị em”

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
00

Tôi sẽ lấy phần này làm ví dụ để chỉ cho bạn cách di chuyển từ phần này sang phần khác của tài liệu

Đi xuống

Thẻ có thể chứa chuỗi và các thẻ khác. Các phần tử này là phần tử con của thẻ. Beautiful Soup cung cấp rất nhiều thuộc tính khác nhau để điều hướng và lặp qua phần con của thẻ

Lưu ý rằng các chuỗi Beautiful Soup không hỗ trợ bất kỳ thuộc tính nào trong số này, bởi vì một chuỗi không thể có con

Điều hướng bằng tên thẻ

The simplest way to navigate the parse tree is to say the name of the tag you want. If you want the tag, just say

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
81:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
01

You can do use this trick again and again to zoom in on a certain part of the parse tree. This code gets the first tag beneath the tag:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
02

Sử dụng tên thẻ làm thuộc tính sẽ chỉ cung cấp cho bạn thẻ đầu tiên theo tên đó

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
03

If you need to get all the Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 51 và

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
83

Con của thẻ có sẵn trong danh sách có tên là

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
51

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
05

The

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 object itself has children. In this case, the tag is the child of the
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 object.:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
06

Một chuỗi không có

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
51, bởi vì nó không thể chứa bất cứ thứ gì

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
07

Thay vì lấy chúng dưới dạng danh sách, bạn có thể lặp lại các phần tử con của thẻ bằng cách sử dụng trình tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
83

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
08

Nếu bạn muốn sửa đổi phần tử con của thẻ, hãy sử dụng các phương pháp được mô tả trong. Không sửa đổi trực tiếp danh sách

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
51. có thể dẫn đến các vấn đề tinh tế và khó phát hiện

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
90

The

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
51 and
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
83 attributes only consider a tag’s direct children. For instance, the tag has a single direct child–the tag:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
09

But the tag itself has a child: the string “The Dormouse’s story”. There’s a sense in which that string is also a child of the tag. The

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
90 attribute lets you iterate over all of a tag’s children, recursively: its direct children, the children of its direct children, and so on:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
10

The tag has only one child, but it has two descendants: the tag and the tag’s child. The

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 object only has one direct child [the tag], but it has a whole lot of descendants:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
11

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52

Nếu một thẻ chỉ có một phần tử con và phần tử con đó là một

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27, thì phần tử con đó được cung cấp dưới dạng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
12

Nếu con duy nhất của một thẻ là một thẻ khác và thẻ đó có một

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52, thì thẻ cha mẹ được coi là có cùng một
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52 như con của nó

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
13

Nếu một thẻ chứa nhiều hơn một thứ, thì không rõ

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52 nên đề cập đến cái gì, vì vậy,
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52 được định nghĩa là
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
002

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
14

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
003 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
004

Nếu có nhiều thứ bên trong một thẻ, bạn vẫn có thể chỉ xem các chuỗi. Sử dụng trình tạo

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
003

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
15

Các chuỗi này có xu hướng có nhiều khoảng trắng thừa, bạn có thể loại bỏ khoảng trắng này bằng cách sử dụng trình tạo

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
006 để thay thế

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
16

Ở đây, các chuỗi bao gồm toàn bộ khoảng trắng bị bỏ qua và khoảng trắng ở đầu và cuối chuỗi bị xóa

Đi lên

Tiếp tục phép loại suy “cây gia đình”, mọi thẻ và mọi chuỗi đều có cha. thẻ chứa nó

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
007

You can access an element’s parent with the

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
007 attribute. In the example “three sisters” document, the tag is the parent of the tag:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
17

The title string itself has a parent: the tag that contains it:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
18

The parent of a top-level tag like is the

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 object itself:

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
19

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
007 của một đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 được định nghĩa là Không có

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
10

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
012

You can iterate over all of an element’s parents with

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
012. This example uses
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
012 to travel from an Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 53 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042. Các phương thức khác có các đối số gần như chính xác giống nhau, vì vậy tôi sẽ chỉ trình bày ngắn gọn về chúng

Một lần nữa, tôi sẽ sử dụng tài liệu “ba chị em” làm ví dụ

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
00

Bằng cách chuyển một bộ lọc đến một đối số như

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042, bạn có thể phóng to các phần của tài liệu mà bạn quan tâm

Các loại bộ lọc

Trước khi nói chi tiết về

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 và các phương thức tương tự, tôi muốn đưa ra các ví dụ về các bộ lọc khác nhau mà bạn có thể chuyển vào các phương thức này. Các bộ lọc này hiển thị lặp đi lặp lại trong toàn bộ API tìm kiếm. Bạn có thể sử dụng chúng để lọc dựa trên tên của thẻ, trên thuộc tính của thẻ, trên văn bản của chuỗi hoặc trên một số kết hợp của những điều này

Một chuỗi

The simplest filter is a string. Pass a string to a search method and Beautiful Soup will perform a match against that exact string. This code finds all the tags in the document:

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
26

Nếu bạn chuyển vào một chuỗi byte, Beautiful Soup sẽ cho rằng chuỗi được mã hóa dưới dạng UTF-8. Thay vào đó, bạn có thể tránh điều này bằng cách chuyển vào một chuỗi Unicode

Một biểu thức chính quy

Nếu bạn chuyển vào một đối tượng biểu thức chính quy, Beautiful Soup sẽ lọc theo biểu thức chính quy đó bằng cách sử dụng phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
045 của nó. Mã này tìm tất cả các thẻ có tên bắt đầu bằng chữ cái “b”;

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
27

Mã này tìm tất cả các thẻ có tên chứa chữ 't'

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
28

Một danh sách

If you pass in a list, Beautiful Soup will allow a string match against any item in that list. This code finds all the Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 35 làm đối số từ khóa sẽ gây ra lỗi cú pháp cho bạn. Kể từ Beautiful Soup 4. 1. 2, bạn có thể tìm kiếm theo lớp CSS bằng đối số từ khóa

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
073

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
14

Như với bất kỳ đối số từ khóa nào, bạn có thể chuyển

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
073 một chuỗi, một biểu thức chính quy, một hàm hoặc
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
046

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
15

rằng một thẻ có thể có nhiều giá trị cho thuộc tính “lớp” của nó. Khi bạn tìm kiếm một thẻ khớp với một lớp CSS nhất định, bạn đang khớp với bất kỳ lớp CSS nào của nó

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
16

Bạn cũng có thể tìm kiếm giá trị chuỗi chính xác của thuộc tính

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
35

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
17

Nhưng tìm kiếm các biến thể của giá trị chuỗi sẽ không hoạt động

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
18

Nếu bạn muốn tìm kiếm các thẻ khớp với hai hoặc nhiều lớp CSS, bạn nên sử dụng bộ chọn CSS

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
19

Trong các phiên bản cũ hơn của Beautiful Soup không có phím tắt

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
073, bạn có thể sử dụng thủ thuật
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
069 đã đề cập ở trên. Tạo một từ điển có giá trị cho “lớp” là chuỗi [hoặc biểu thức chính quy hoặc bất kỳ thứ gì] mà bạn muốn tìm kiếm

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
20

Đối số
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
058

Với

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
058, bạn có thể tìm kiếm chuỗi thay vì thẻ. Như với
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
062 và các đối số từ khóa, bạn có thể chuyển vào , , , , hoặc. Dưới đây là một số ví dụ

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
21

Although

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
058 is for finding strings, you can combine it with arguments that find tags: Beautiful Soup will find all tags whose
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52 matches your value for
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
058. This code finds the Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 52 is “Elsie”:

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
22

Đối số

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
058 mới trong Beautiful Soup 4. 4. 0. Trong các phiên bản trước, nó được gọi là
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
087

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
23

Đối số
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
088

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 trả về tất cả các thẻ và chuỗi phù hợp với bộ lọc của bạn. Quá trình này có thể mất một lúc nếu tài liệu lớn. Nếu bạn không cần tất cả các kết quả, bạn có thể nhập một số cho
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
088. Điều này hoạt động giống như từ khóa LIMIT trong SQL. Nó yêu cầu Beautiful Soup ngừng thu thập kết quả sau khi tìm thấy một số nhất định

Có ba liên kết trong tài liệu "ba chị em", nhưng mã này chỉ tìm thấy hai liên kết đầu tiên

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
24

Đối số
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
091

Nếu bạn gọi cho

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
092, Beautiful Soup sẽ kiểm tra tất cả con cháu của
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
093. con của nó, con của con nó, v.v. Nếu bạn chỉ muốn Beautiful Soup xem xét những đứa trẻ trực tiếp, bạn có thể vượt qua
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
094. Xem sự khác biệt ở đây

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
25

Đây là một phần của tài liệu

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
26

The tag is beneath the tag, but it’s not directly beneath the tag: the tag is in the way. Beautiful Soup finds the tag when it’s allowed to look at all descendants of the tag, but when

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
094 restricts it to the tag’s immediate children, it finds nothing.

Beautiful Soup cung cấp rất nhiều phương pháp tìm kiếm trên cây [được trình bày bên dưới] và hầu hết chúng đều lấy các đối số giống như

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042. ________ 1062, ________ 1069, ______ 1058, ________ 1088 và các đối số từ khóa. Nhưng đối số
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
091 thì khác.
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53 là những phương pháp duy nhất hỗ trợ nó. Chuyển
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
094 thành một phương thức như
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
105 sẽ không hữu ích lắm

Gọi một thẻ giống như gọi
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042

Bởi vì

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 là phương thức phổ biến nhất trong API tìm kiếm Beautiful Soup, bạn có thể sử dụng lối tắt cho nó. Nếu bạn coi đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 hoặc đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 như thể nó là một hàm, thì điều đó cũng giống như gọi
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 trên đối tượng đó. Hai dòng mã này là tương đương

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
27

Hai dòng này cũng tương đương

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
28

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53

chữ ký phương thức. tìm thấy[, , , , ]

The

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 method scans the entire document looking for results, but sometimes you only want to find one result. If you know a document only has one tag, it’s a waste of time to scan the entire document looking for more. Rather than passing in
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
113 every time you call
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
114, you can use the
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53 method. These two lines of code are nearly equivalent:

mysql> mysql> insert into escape_characters_demo[id, string]
       values[1, 'test\'s'],
       [2, 'test\"s'],
       [3, 'test\`s'],
       [4, 'test\'s and best\'s'],
       [5, 'test\"s and best\"s'],
       [6, 'test\"s and best\'s'];

mysql> select * from escape_characters_demo;
+------+-------------------+
| id   | string            |
+------+-------------------+
|    1 | test's            |
|    2 | test"s            |
|    3 | test`s            |
|    4 | test's and best's |
|    5 | test"s and best"s |
|    6 | test"s and best's |
+------+-------------------+
29

Sự khác biệt duy nhất là

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 trả về một danh sách chứa kết quả duy nhất và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53 chỉ trả về kết quả

Nếu

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 không tìm thấy gì, nó sẽ trả về một danh sách trống. Nếu ________ 753 không tìm thấy gì, nó sẽ trả về ________ 1002

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
00

Hãy nhớ thủ thuật

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
121 từ ?

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
01

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
105 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
124

chữ ký phương thức. find_parents[, , , , ]

chữ ký phương thức. find_parent[, , , ]

Tôi đã dành rất nhiều thời gian ở trên để viết về

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53. API Beautiful Soup định nghĩa mười phương pháp khác để tìm kiếm cây, nhưng đừng sợ. Năm trong số các phương thức này về cơ bản giống với
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 và năm phương thức còn lại về cơ bản giống với
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53. Sự khác biệt duy nhất là chúng tìm kiếm ở bộ phận nào của cây

Trước tiên, hãy xem xét

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
105 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
124. Hãy nhớ rằng
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 và
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53 đi xuống cây, nhìn vào hậu duệ của thẻ. Các phương pháp này làm ngược lại. họ leo lên cây, nhìn vào cha của thẻ [hoặc chuỗi]. Thử xem nào, bắt đầu từ sợi dây chôn sâu trong tài liệu “ba cô con gái”

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
02

One of the three # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# # 03

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
141 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
142

chữ ký phương thức. find_previous_siblings[, , , , ]

chữ ký phương thức. find_previous_sibling[, , , ]

Các phương thức này sử dụng để lặp lại các phần tử anh em của phần tử đứng trước nó trong cây. Phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
141 trả về tất cả các anh chị em phù hợp và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
142 chỉ trả về anh chị em đầu tiên

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
04

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
145 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
146

chữ ký phương thức. find_all_next[, , , , ]

chữ ký phương thức. find_next[, , , ]

Các phương thức này sử dụng để lặp lại bất kỳ thẻ và chuỗi nào xuất hiện sau nó trong tài liệu. Phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
145 trả về tất cả các kết quả khớp và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
146 chỉ trả về kết quả khớp đầu tiên

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
05

In the first example, the string “Elsie” showed up, even though it was contained within the # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# # 06

The call to

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
153 found the first paragraph in the document [the one with class=”title”], but it also finds the second paragraph, the

tag that contains the Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 06 có phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
155 sử dụng gói SoupSieve để chạy bộ chọn CSS đối với tài liệu được phân tích cú pháp và trả về tất cả các phần tử phù hợp.
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 có một phương thức tương tự chạy bộ chọn CSS đối với nội dung của một thẻ

[Tích hợp SoupSieve đã được thêm vào Beautiful Soup 4. 7. 0. Các phiên bản trước cũng có phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
155, nhưng chỉ hỗ trợ các bộ chọn CSS được sử dụng phổ biến nhất. Nếu bạn đã cài đặt Beautiful Soup đến
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
08, SoupSieve đã được cài đặt cùng lúc, vì vậy bạn không phải làm gì thêm. ]

Tài liệu SoupSieve liệt kê tất cả các bộ chọn CSS hiện được hỗ trợ, nhưng đây là một số điều cơ bản

Bạn có thể tìm thấy các thẻ

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
07

Tìm các thẻ bên dưới các thẻ khác

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
08

Tìm các thẻ ngay bên dưới các thẻ khác

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
09

Tìm anh chị em của thẻ

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
10

Tìm thẻ theo lớp CSS

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
11

Tìm thẻ theo ID

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
12

Tìm các thẻ khớp với bất kỳ bộ chọn nào từ danh sách các bộ chọn

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
13

Kiểm tra sự tồn tại của một thuộc tính

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
14

Tìm thẻ theo giá trị thuộc tính

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
15

Ngoài ra còn có một phương pháp gọi là

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
159, chỉ tìm thấy thẻ đầu tiên khớp với bộ chọn

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
16

Nếu bạn đã phân tích cú pháp XML xác định không gian tên, thì bạn có thể sử dụng chúng trong bộ chọn CSS

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
17

Khi xử lý bộ chọn CSS sử dụng không gian tên, Beautiful Soup luôn cố gắng sử dụng các tiền tố không gian tên có ý nghĩa dựa trên những gì nó thấy trong khi phân tích cú pháp tài liệu. Bạn luôn có thể cung cấp từ điển viết tắt của riêng bạn

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
18

Tất cả nội dung bộ chọn CSS này là một tiện ích cho những người đã biết cú pháp bộ chọn CSS. Bạn có thể làm tất cả những điều này với API Beautiful Soup. Và nếu bộ chọn CSS là tất cả những gì bạn cần, bạn nên phân tích cú pháp tài liệu bằng lxml. nó nhanh hơn rất nhiều. Nhưng điều này cho phép bạn kết hợp các bộ chọn CSS với API Beautiful Soup

Sửa đổi cây

Điểm mạnh chính của Beautiful Soup là tìm kiếm cây phân tích cú pháp, nhưng bạn cũng có thể sửa đổi cây và viết các thay đổi của mình dưới dạng tài liệu HTML hoặc XML mới

Thay đổi tên thẻ và thuộc tính

Tôi đã đề cập đến điều này trước đó, trong năm 2010, nhưng nó lặp đi lặp lại. Bạn có thể đổi tên thẻ, thay đổi giá trị của thuộc tính, thêm thuộc tính mới và xóa thuộc tính

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
19

Sửa đổi
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52

Nếu bạn đặt thuộc tính

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
52 của thẻ thành một chuỗi mới, thì nội dung của thẻ sẽ được thay thế bằng chuỗi đó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
20

Hãy cẩn thận. nếu thẻ chứa các thẻ khác, chúng và tất cả nội dung của chúng sẽ bị hủy

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
162

Bạn có thể thêm vào nội dung của thẻ bằng

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
163. Nó hoạt động giống như gọi
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
164 trong danh sách Python

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
21

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
165

Bắt đầu từ Beautiful Soup 4. 7. 0,

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 cũng hỗ trợ một phương thức có tên là
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
167, phương thức này thêm mọi phần tử của danh sách vào một
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26, theo thứ tự

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
22

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
169 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
170

Nếu bạn cần thêm một chuỗi vào tài liệu, không vấn đề gì – bạn có thể chuyển một chuỗi Python vào

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
162 hoặc bạn có thể gọi hàm tạo
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
23

Nếu bạn muốn tạo một bình luận hoặc một số lớp con khác của

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27, chỉ cần gọi hàm tạo

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
24

[Đây là tính năng mới trong Beautiful Soup 4. 4. 0. ]

Nếu bạn cần tạo một thẻ hoàn toàn mới thì sao?

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
25

Chỉ đối số đầu tiên, tên thẻ, là bắt buộc

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
175

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
176 cũng giống như
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
163, ngoại trừ phần tử mới không nhất thiết phải ở cuối phần tử mẹ của nó là
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
51. Nó sẽ được chèn vào bất kỳ vị trí số nào bạn nói. Nó hoạt động giống như
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
179 trong danh sách Python

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
26

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
180 và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
181

Phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
180 chèn các thẻ hoặc chuỗi ngay trước một thứ khác trong cây phân tích cú pháp

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
27

Phương thức

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
181 chèn các thẻ hoặc chuỗi ngay sau một thứ khác trong cây phân tích cú pháp

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
28

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
184

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
185 xóa nội dung của thẻ

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
29

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
186

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
187 xóa thẻ hoặc chuỗi khỏi cây. Nó trả về thẻ hoặc chuỗi đã được trích xuất

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
30

Tại thời điểm này, bạn thực sự có hai cây phân tích. một bắt nguồn từ đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 mà bạn đã sử dụng để phân tích cú pháp tài liệu và một bắt nguồn từ thẻ đã được trích xuất. Bạn có thể tiếp tục gọi
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
189 trên phần tử con của phần tử mà bạn đã trích xuất

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
31

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
190

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
191 xóa thẻ khỏi cây, sau đó phá hủy hoàn toàn thẻ và nội dung của thẻ

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
32

Hành vi của một

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 bị phân tách không được xác định và bạn không nên sử dụng nó cho bất cứ điều gì. Nếu bạn không chắc thứ gì đó đã bị phân hủy hay chưa, bạn có thể kiểm tra thuộc tính
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
194 của nó [mới trong Beautiful Soup 4. 9. 0]

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
33

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
195

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
196 xóa thẻ hoặc chuỗi khỏi cây và thay thế bằng một hoặc nhiều thẻ hoặc chuỗi bạn chọn

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
34

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
195 trả về thẻ hoặc chuỗi đã được thay thế để bạn có thể kiểm tra hoặc thêm lại vào phần khác của cây

Khả năng chuyển nhiều đối số vào replace_with[] là tính năng mới trong Beautiful Soup 4. 10. 0

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
198

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
199 bọc một phần tử trong thẻ bạn chỉ định. Nó trả về trình bao bọc mới

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
35

Phương pháp này mới trong Beautiful Soup 4. 0. 5

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
100

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
101 ngược lại với
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
198. Nó thay thế một thẻ bằng bất cứ thứ gì bên trong thẻ đó. Nó tốt cho việc loại bỏ đánh dấu

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
36

Giống như

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
195,
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
100 trả về thẻ đã được thay thế

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
105

Sau khi gọi một loạt các phương thức sửa đổi cây phân tích cú pháp, bạn có thể kết thúc bằng hai hoặc nhiều đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 cạnh nhau. Beautiful Soup không có bất kỳ vấn đề nào với điều này, nhưng vì nó không thể xảy ra trong một tài liệu mới được phân tích cú pháp nên bạn có thể không mong đợi hành vi như sau

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
37

Bạn có thể gọi

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
107 để dọn sạch cây phân tích cú pháp bằng cách hợp nhất các chuỗi liền kề

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
38

Phương pháp này mới trong Beautiful Soup 4. 8. 0

đầu ra

in đẹp

Phương thức

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
108 sẽ biến cây phân tích cú pháp Beautiful Soup thành một chuỗi Unicode được định dạng độc đáo, với một dòng riêng cho mỗi thẻ và mỗi chuỗi

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
39

Bạn có thể gọi

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
108 trên đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 cấp cao nhất hoặc trên bất kỳ đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 nào của nó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
40

Vì nó thêm khoảng trắng [ở dạng dòng mới], nên

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
108 thay đổi ý nghĩa của tài liệu HTML và không nên được sử dụng để định dạng lại tài liệu. Mục tiêu của
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
108 là giúp bạn hiểu một cách trực quan cấu trúc của các tài liệu mà bạn làm việc với

In không đẹp

Nếu bạn chỉ muốn một chuỗi, không có định dạng ưa thích, bạn có thể gọi

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
114 trên một đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 hoặc trên một
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 bên trong nó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
41

Hàm

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
114 trả về một chuỗi được mã hóa bằng UTF-8. Xem các tùy chọn khác

Bạn cũng có thể gọi

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
118 để lấy bytestring và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
119 để lấy Unicode

bộ định dạng đầu ra

Nếu bạn cung cấp cho Beautiful Soup một tài liệu chứa các thực thể HTML như “&lquot;”, chúng sẽ được chuyển đổi thành các ký tự Unicode

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
42

Sau đó, nếu bạn chuyển đổi tài liệu thành một chuỗi ký tự, thì các ký tự Unicode sẽ được mã hóa thành UTF-8. Bạn sẽ không lấy lại được các thực thể HTML

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
43

By default, the only characters that are escaped upon output are bare ampersands and angle brackets. These get turned into “&”, “”, so that Beautiful Soup doesn’t inadvertently generate invalid HTML or XML:

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
44

Bạn có thể thay đổi hành vi này bằng cách cung cấp một giá trị cho đối số

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
120 thành
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
108,
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
118 hoặc
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
119. Beautiful Soup nhận ra năm giá trị có thể có cho
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
120

Mặc định là

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
125. Chuỗi sẽ chỉ được xử lý đủ để đảm bảo rằng Beautiful Soup tạo HTML/XML hợp lệ

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
45

Nếu bạn vượt qua

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
126, Beautiful Soup sẽ chuyển đổi các ký tự Unicode thành các thực thể HTML bất cứ khi nào có thể

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
46

Nếu bạn vượt qua

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
127, nó tương tự như
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
126, nhưng Beautiful Soup sẽ bỏ qua dấu gạch chéo trong các thẻ trống HTML như “br”

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
47

Ngoài ra, bất kỳ thuộc tính nào có giá trị là chuỗi rỗng sẽ trở thành thuộc tính boolean kiểu HTML

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
48

[Hành vi này là mới kể từ Beautiful Soup 4. 10. 0. ]

Nếu bạn vượt qua

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
129, Beautiful Soup sẽ không sửa đổi chuỗi nào ở đầu ra. Đây là tùy chọn nhanh nhất, nhưng nó có thể dẫn đến việc Beautiful Soup tạo HTML/XML không hợp lệ, như trong các ví dụ sau

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
49

Nếu bạn cần kiểm soát tinh vi hơn đối với đầu ra của mình, bạn có thể sử dụng lớp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
130 của Beautiful Soup. Đây là một trình định dạng chuyển đổi chuỗi thành chữ hoa, cho dù chúng xuất hiện trong một nút văn bản hay trong một giá trị thuộc tính

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
50

Đây là một trình định dạng giúp tăng độ thụt đầu dòng khi in đẹp

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
51

Phân lớp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
131 hoặc
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
132 sẽ cung cấp cho bạn nhiều quyền kiểm soát hơn đối với đầu ra. Ví dụ: Beautiful Soup sắp xếp các thuộc tính trong mọi thẻ theo mặc định

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
52

Để tắt tính năng này, bạn có thể phân lớp phương thức

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
133, phương thức này kiểm soát thuộc tính nào được xuất và theo thứ tự nào. Việc triển khai này cũng lọc ra thuộc tính có tên là “m” bất cứ khi nào nó xuất hiện

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
53

Một cảnh báo cuối cùng. nếu bạn tạo một đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
75, văn bản bên trong đối tượng đó luôn được trình bày chính xác như nó xuất hiện, không có định dạng. Beautiful Soup sẽ gọi hàm thay thế thực thể của bạn, chỉ trong trường hợp bạn đã viết một hàm tùy chỉnh đếm tất cả các chuỗi trong tài liệu hoặc thứ gì đó, nhưng nó sẽ bỏ qua giá trị trả về

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
54

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
135

Nếu bạn chỉ muốn văn bản con người có thể đọc được bên trong tài liệu hoặc thẻ, bạn có thể sử dụng phương pháp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
135. Nó trả về tất cả văn bản trong tài liệu hoặc bên dưới thẻ, dưới dạng một chuỗi Unicode

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
55

Bạn có thể chỉ định một chuỗi được sử dụng để nối các đoạn văn bản lại với nhau

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
56

Bạn có thể yêu cầu Beautiful Soup loại bỏ khoảng trắng từ đầu và cuối mỗi đoạn văn bản

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
57

Nhưng tại thời điểm đó, bạn có thể muốn sử dụng trình tạo thay thế và tự xử lý văn bản

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
58

As of Beautiful Soup version 4.9.0, when lxml or html.parser are in use, the contents of , , and tags are generally not considered to be ‘text’, since those tags are not part of the human-visible content of the page.

Kể từ phiên bản Beautiful Soup 4. 10. 0, bạn có thể gọi get_text[],. chuỗi, hoặc. striped_strings trên một đối tượng NavigableString. Nó sẽ tự trả về đối tượng hoặc không có gì, vì vậy lý do duy nhất để làm điều này là khi bạn đang lặp qua một danh sách hỗn hợp

Chỉ định trình phân tích cú pháp để sử dụng

Nếu bạn chỉ cần phân tích cú pháp một số HTML, bạn có thể kết xuất phần đánh dấu vào hàm tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 và có thể sẽ ổn thôi. Beautiful Soup sẽ chọn một trình phân tích cú pháp cho bạn và phân tích dữ liệu. Nhưng có một vài đối số bổ sung mà bạn có thể chuyển vào hàm tạo để thay đổi trình phân tích cú pháp nào được sử dụng

Đối số đầu tiên của hàm tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 là một chuỗi hoặc một tệp xử lý mở–phần đánh dấu bạn muốn phân tích cú pháp. Đối số thứ hai là cách bạn muốn đánh dấu được phân tích cú pháp

Nếu bạn không chỉ định bất cứ điều gì, bạn sẽ nhận được trình phân tích cú pháp HTML tốt nhất đã được cài đặt. Beautiful Soup xếp hạng trình phân tích cú pháp của lxml là tốt nhất, sau đó là html5lib, sau đó là trình phân tích cú pháp tích hợp sẵn của Python. Bạn có thể ghi đè điều này bằng cách chỉ định một trong những điều sau đây

  • Loại đánh dấu bạn muốn phân tích. Hiện tại được hỗ trợ là “html”, “xml” và “html5”

  • Tên của thư viện trình phân tích cú pháp bạn muốn sử dụng. Các tùy chọn được hỗ trợ hiện tại là “lxml”, “html5lib” và “html. trình phân tích cú pháp” [Trình phân tích cú pháp HTML tích hợp sẵn của Python]

Phần đối chiếu các trình phân tích cú pháp được hỗ trợ

Nếu bạn chưa cài đặt trình phân tích cú pháp phù hợp, Beautiful Soup sẽ bỏ qua yêu cầu của bạn và chọn một trình phân tích cú pháp khác. Hiện tại, trình phân tích cú pháp XML được hỗ trợ duy nhất là lxml. Nếu bạn chưa cài đặt lxml, yêu cầu trình phân tích cú pháp XML sẽ không cung cấp cho bạn và yêu cầu “lxml” cũng sẽ không hoạt động

Sự khác biệt giữa các trình phân tích cú pháp

Beautiful Soup trình bày cùng một giao diện cho một số trình phân tích cú pháp khác nhau, nhưng mỗi trình phân tích cú pháp lại khác nhau. Các trình phân tích cú pháp khác nhau sẽ tạo các cây phân tích cú pháp khác nhau từ cùng một tài liệu. Sự khác biệt lớn nhất là giữa trình phân tích cú pháp HTML và trình phân tích cú pháp XML. Đây là một tài liệu ngắn, được phân tích dưới dạng HTML bằng trình phân tích cú pháp đi kèm với Python

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
59

Since a standalone tag is not valid HTML, html.parser turns it into a tag pair.

Here’s the same document parsed as XML [running this requires that you have lxml installed]. Note that the standalone tag is left alone, and that the document is given an XML declaration instead of being put into an tag.:

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
60

Cũng có sự khác biệt giữa các trình phân tích cú pháp HTML. Nếu bạn cung cấp cho Beautiful Soup một tài liệu HTML được định dạng hoàn hảo, những điểm khác biệt này sẽ không thành vấn đề. Một trình phân tích cú pháp sẽ nhanh hơn trình phân tích cú pháp khác, nhưng tất cả chúng sẽ cung cấp cho bạn cấu trúc dữ liệu giống hệt như tài liệu HTML gốc

But if the document is not perfectly-formed, different parsers will give different results. Here’s a short, invalid document parsed using lxml’s HTML parser. Note that the # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# # 61

Đây là cùng một tài liệu được phân tích cú pháp bằng html5lib

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
62

Instead of ignoring the dangling

tag, html5lib pairs it with an opening

tag. html5lib also adds an empty tag; lxml didn’t bother.

Đây là cùng một tài liệu được phân tích cú pháp bằng trình phân tích cú pháp HTML tích hợp của Python

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
63

Like lxml, this parser ignores the closing

tag. Unlike html5lib or lxml, this parser makes no attempt to create a well-formed HTML document by adding or tags.

Since the document “Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 06. Điều đó sẽ làm giảm khả năng người dùng của bạn phân tích tài liệu khác với cách bạn phân tích cú pháp đó

mã hóa

Bất kỳ tài liệu HTML hoặc XML nào được viết bằng một mã hóa cụ thể như ASCII hoặc UTF-8. Nhưng khi bạn tải tài liệu đó vào Beautiful Soup, bạn sẽ phát hiện ra rằng nó đã được chuyển đổi sang Unicode

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
64

Nó không phải là ma thuật. [Điều đó chắc chắn sẽ được tốt đẹp. ] Beautiful Soup sử dụng thư viện con được gọi để phát hiện mã hóa tài liệu và chuyển đổi nó thành Unicode. Mã hóa được tự động phát hiện có sẵn dưới dạng thuộc tính

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
140 của đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
65

Unicode, Dammit đoán đúng hầu hết thời gian, nhưng đôi khi nó mắc lỗi. Đôi khi nó đoán đúng, nhưng chỉ sau khi tìm kiếm từng byte tài liệu mất rất nhiều thời gian. Nếu bạn tình cờ biết trước mã hóa của tài liệu, bạn có thể tránh nhầm lẫn và chậm trễ bằng cách chuyển mã đó cho hàm tạo ________ 706 dưới dạng ________ 3143

Đây là một tài liệu được viết bằng ISO-8859-8. Tài liệu ngắn đến mức Unicode, Chết tiệt không thể khóa nó và xác định nhầm nó là ISO-8859-7

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
66

Chúng ta có thể khắc phục điều này bằng cách chuyển đúng

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
143

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
67

Nếu bạn không biết mã hóa chính xác là gì, nhưng bạn biết rằng Unicode, Chết tiệt đang đoán sai, bạn có thể chuyển các dự đoán sai thành

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
145

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
68

Windows-1255 không chính xác 100%, nhưng mã hóa đó là bộ siêu tương thích của ISO-8859-8, do đó, nó đủ gần. [______3145 là một tính năng mới trong Beautiful Soup 4. 4. 0. ]

Trong một số ít trường hợp [thường là khi tài liệu UTF-8 chứa văn bản được viết bằng một mã hóa hoàn toàn khác], cách duy nhất để lấy Unicode có thể là thay thế một số ký tự bằng ký tự Unicode đặc biệt “THỰC PHẨM THAY THẾ” [U+FFFD, �]. Nếu Unicode, Dammit cần làm điều này, nó sẽ đặt thuộc tính

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
147 thành
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
046 trên đối tượng
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
149 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06. Điều này cho bạn biết rằng biểu diễn Unicode không phải là biểu diễn chính xác của bản gốc–một số dữ liệu đã bị mất. Nếu một tài liệu chứa �, nhưng
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
147 là
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
049, bạn sẽ biết rằng � ban đầu ở đó [như trong đoạn này] và không đại diện cho dữ liệu bị thiếu

mã hóa đầu ra

Khi bạn viết ra một tài liệu từ Beautiful Soup, bạn sẽ nhận được một tài liệu UTF-8, ngay cả khi ban đầu tài liệu đó không ở dạng UTF-8. Đây là một tài liệu được viết bằng bảng mã Latin-1

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
69

Note that the tag has been rewritten to reflect the fact that the document is now in UTF-8.

Nếu bạn không muốn UTF-8, bạn có thể chuyển mã hóa vào

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
108

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
70

Bạn cũng có thể gọi mã hóa [] trên đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 hoặc bất kỳ phần tử nào trong súp, giống như thể đó là một chuỗi Python

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
71

Bất kỳ ký tự nào không thể được biểu diễn trong mã hóa bạn đã chọn sẽ được chuyển đổi thành các tham chiếu thực thể XML dạng số. Đây là tài liệu bao gồm ký tự Unicode SNOWMAN

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
72

Ký tự SNOWMAN có thể là một phần của tài liệu UTF-8 [trông giống như ☃], nhưng không có biểu diễn nào cho ký tự đó trong ISO-Latin-1 hoặc ASCII, vì vậy ký tự này được chuyển đổi thành “☃” cho các mã hóa đó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
73

Unicode, chết tiệt

Bạn có thể sử dụng Unicode, Dammit mà không cần sử dụng Beautiful Soup. Nó hữu ích bất cứ khi nào bạn có dữ liệu ở dạng mã hóa không xác định và bạn chỉ muốn nó trở thành Unicode

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
74

Unicode, dự đoán của Dammit sẽ chính xác hơn rất nhiều nếu bạn cài đặt một trong những thư viện Python này.

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
155,
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
156 hoặc
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
157. Bạn càng cung cấp nhiều dữ liệu Unicode, Chết tiệt, nó sẽ đoán càng chính xác. Nếu bạn có những nghi ngờ của riêng mình về việc mã hóa có thể là gì, bạn có thể chuyển chúng vào dưới dạng danh sách

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
75

Unicode, Dammit có hai tính năng đặc biệt mà Beautiful Soup không sử dụng

báo giá thông minh

Bạn có thể sử dụng Unicode, Dammit để chuyển đổi các trích dẫn thông minh của Microsoft sang các thực thể HTML hoặc XML

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
76

Bạn cũng có thể chuyển đổi dấu ngoặc kép thông minh của Microsoft thành dấu ngoặc kép ASCII

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
77

Hy vọng rằng bạn sẽ thấy tính năng này hữu ích, nhưng Beautifulsoup không sử dụng nó. Beautiful Soup thích hành vi mặc định hơn, đó là chuyển đổi các trích dẫn thông minh của Microsoft thành các ký tự Unicode cùng với mọi thứ khác

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
78

mã hóa không nhất quán

Đôi khi, một tài liệu chủ yếu ở dạng UTF-8, nhưng chứa các ký tự Windows-1252, chẳng hạn như [một lần nữa] dấu ngoặc kép thông minh của Microsoft. Điều này có thể xảy ra khi một trang web bao gồm dữ liệu từ nhiều nguồn. Bạn có thể sử dụng

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
158 để biến một tài liệu như vậy thành UTF-8 thuần túy. Đây là một ví dụ đơn giản

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
79

Tài liệu này là một mớ hỗn độn. Người tuyết ở dạng UTF-8 và dấu ngoặc kép ở dạng Windows-1252. Bạn có thể hiển thị người tuyết hoặc dấu ngoặc kép, nhưng không thể hiển thị cả hai

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
80

Giải mã tài liệu dưới dạng UTF-8 sẽ tăng

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
159 và giải mã nó dưới dạng Windows-1252 sẽ khiến bạn vô nghĩa. May mắn thay,
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
158 sẽ chuyển đổi chuỗi thành UTF-8 thuần túy, cho phép bạn giải mã nó thành Unicode và hiển thị đồng thời người tuyết và dấu ngoặc kép

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
81

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
158 chỉ biết cách xử lý Windows-1252 được nhúng trong UTF-8 [hoặc ngược lại, tôi cho là vậy], nhưng đây là trường hợp phổ biến nhất

Lưu ý rằng bạn phải biết gọi

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
158 trên dữ liệu của mình trước khi chuyển dữ liệu đó vào
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 hoặc hàm tạo
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
149. Beautiful Soup giả định rằng một tài liệu có một mã hóa duy nhất, bất kể nó có thể là gì. Nếu bạn chuyển cho nó một tài liệu chứa cả UTF-8 và Windows-1252, thì có khả năng nó sẽ nghĩ rằng toàn bộ tài liệu là Windows-1252 và tài liệu xuất hiện trông giống như
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
165

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
158 mới trong Beautiful Soup 4. 1. 0

số dòng

Trình phân tích cú pháp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
167 và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
168 có thể theo dõi vị trí của mỗi Thẻ được tìm thấy trong tài liệu gốc. Bạn có thể truy cập thông tin này dưới dạng
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
169 [số dòng] và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
170 [vị trí của thẻ bắt đầu trong một dòng]

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
82

Lưu ý rằng hai trình phân tích cú pháp có ý nghĩa hơi khác nhau bởi

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
171 và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
172. Đối với html. trình phân tích cú pháp, những con số này biểu thị vị trí của dấu nhỏ hơn ban đầu. Đối với html5lib, những con số này biểu thị vị trí của dấu lớn hơn cuối cùng

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
83

Bạn có thể tắt tính năng này bằng cách chuyển hàm tạo

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
173

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
84

Tính năng này mới trong 4. 8. 1 và trình phân tích cú pháp dựa trên lxml không hỗ trợ nó

So sánh các đối tượng cho bình đẳng

Beautiful Soup says that two

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 or
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 objects are equal when they represent the same HTML or XML markup. In this example, the two tags are treated as equal, even though they live in different parts of the object tree, because they both look like “pizza”:

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
85

Nếu bạn muốn xem liệu hai biến có tham chiếu chính xác đến cùng một đối tượng hay không, hãy sử dụng

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
86

Sao chép các đối tượng Beautiful Soup

Bạn có thể sử dụng

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
176 để tạo bản sao của bất kỳ
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 nào

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
87

Bản sao được coi là ngang bằng với bản gốc, vì nó thể hiện cùng một đánh dấu như bản gốc, nhưng nó không phải là cùng một đối tượng

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
88

Sự khác biệt thực sự duy nhất là bản sao hoàn toàn tách rời khỏi cây đối tượng Beautiful Soup ban đầu, giống như thể

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
186 đã được gọi trên đó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
89

Điều này là do hai đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 khác nhau không thể chiếm cùng một không gian cùng một lúc

Tùy chỉnh trình phân tích cú pháp nâng cao

Beautiful Soup cung cấp một số cách để tùy chỉnh cách trình phân tích cú pháp xử lý HTML và XML đến. Phần này bao gồm các kỹ thuật tùy chỉnh được sử dụng phổ biến nhất

Chỉ phân tích cú pháp một phần của tài liệu

Let’s say you want to use Beautiful Soup look at a document’s Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie 06 constructor as the

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
184 argument.

[Lưu ý rằng tính năng này sẽ không hoạt động nếu bạn đang sử dụng trình phân tích cú pháp html5lib. Nếu bạn sử dụng html5lib, toàn bộ tài liệu sẽ được phân tích cú pháp, không có vấn đề gì. Điều này là do html5lib liên tục sắp xếp lại cây phân tích cú pháp khi nó hoạt động và nếu một phần nào đó của tài liệu không thực sự đưa nó vào cây phân tích cú pháp, nó sẽ bị lỗi. Để tránh nhầm lẫn, trong các ví dụ bên dưới, tôi sẽ buộc Beautiful Soup sử dụng trình phân tích cú pháp tích hợp sẵn của Python. ]

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
181

Lớp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
181 lấy các đối số giống như một phương thức điển hình từ. , , , và. Đây là ba đối tượng
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
181

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
90

Tôi sẽ mang tài liệu “ba chị em” trở lại một lần nữa và chúng ta sẽ xem tài liệu trông như thế nào khi nó được phân tích cú pháp với ba đối tượng

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
181 này

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
91

Bạn cũng có thể chuyển một

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
181 vào bất kỳ phương thức nào được đề cập trong. Điều này có lẽ không hữu ích lắm, nhưng tôi nghĩ tôi sẽ đề cập đến nó

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
92

Tùy chỉnh các thuộc tính đa giá trị

Trong tài liệu HTML, một thuộc tính như

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
35 được cung cấp một danh sách các giá trị và một thuộc tính như
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
059 được cung cấp một giá trị, bởi vì đặc tả HTML xử lý các thuộc tính đó theo cách khác

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
93

Bạn có thể tắt tính năng này bằng cách chuyển vào

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
41. Hơn tất cả các thuộc tính sẽ được cung cấp một giá trị duy nhất

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
94

Bạn có thể tùy chỉnh hành vi này một chút bằng cách chuyển vào từ điển cho

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
44. Nếu bạn cần điều này, hãy xem
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
194 để xem cấu hình mà Beautiful Soup sử dụng theo mặc định, dựa trên đặc tả HTML

[Đây là tính năng mới trong Beautiful Soup 4. 8. 0. ]

Xử lý các thuộc tính trùng lặp

Khi sử dụng trình phân tích cú pháp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
167, bạn có thể sử dụng đối số hàm tạo
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
196 để tùy chỉnh những gì Beautiful Soup thực hiện khi nó gặp một thẻ xác định cùng một thuộc tính nhiều lần

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
95

Hành vi mặc định là sử dụng giá trị cuối cùng được tìm thấy cho thẻ

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
96

Với

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
197, bạn có thể yêu cầu Beautiful Soup sử dụng giá trị đầu tiên được tìm thấy và bỏ qua phần còn lại

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
97

[lxml và html5lib luôn làm theo cách này; hành vi của chúng không thể được định cấu hình từ bên trong Beautiful Soup. ]

Nếu cần thêm, bạn có thể chuyển vào một hàm được gọi trên mỗi giá trị trùng lặp

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
98

[Đây là tính năng mới trong Beautiful Soup 4. 9. 1. ]

Khởi tạo các lớp con tùy chỉnh

Khi trình phân tích cú pháp nói với Beautiful Soup về một thẻ hoặc một chuỗi, Beautiful Soup sẽ khởi tạo một đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27 để chứa thông tin đó. Thay vì hành vi mặc định đó, bạn có thể yêu cầu Beautiful Soup khởi tạo các lớp con của
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
27, các lớp con mà bạn xác định bằng hành vi tùy chỉnh

from bs4 import BeautifulSoup
soup = BeautifulSoup[html_doc, 'html.parser']

print[soup.prettify[]]
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #
99

Điều này có thể hữu ích khi kết hợp Beautiful Soup vào khung thử nghiệm

[Đây là tính năng mới trong Beautiful Soup 4. 8. 1. ]

Xử lý sự cố

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
202

Nếu bạn gặp khó khăn trong việc hiểu Beautiful Soup làm gì với một tài liệu, hãy chuyển tài liệu đó vào hàm

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
202. [Mới trong Súp Đẹp 4. 2. 0. ] Beautiful Soup sẽ in ra một báo cáo cho bạn biết các trình phân tích cú pháp khác nhau xử lý tài liệu như thế nào và cho bạn biết nếu bạn đang thiếu một trình phân tích cú pháp mà Beautiful Soup có thể đang sử dụng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
00

Chỉ cần nhìn vào đầu ra của chẩn đoán [] có thể chỉ cho bạn cách giải quyết vấn đề. Ngay cả khi không, bạn có thể dán đầu ra của

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
202 khi yêu cầu trợ giúp

Lỗi khi phân tích tài liệu

Có hai loại lỗi phân tích cú pháp khác nhau. Có những sự cố xảy ra khi bạn cung cấp tài liệu cho Beautiful Soup và nó đưa ra một ngoại lệ, thường là

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
205. Và có hành vi không mong muốn, trong đó cây phân tích Beautiful Soup trông khác rất nhiều so với tài liệu được sử dụng để tạo ra nó

Hầu như không có vấn đề nào trong số này trở thành vấn đề với Beautiful Soup. Điều này không phải vì Beautiful Soup là một phần mềm được viết tốt một cách đáng kinh ngạc. Đó là bởi vì Beautiful Soup không bao gồm bất kỳ mã phân tích cú pháp nào. Thay vào đó, nó dựa vào các trình phân tích cú pháp bên ngoài. Nếu một trình phân tích cú pháp không hoạt động trên một tài liệu nhất định, giải pháp tốt nhất là thử một trình phân tích cú pháp khác. Xem để biết chi tiết và so sánh trình phân tích cú pháp

Các lỗi phân tích cú pháp phổ biến nhất là

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
206 và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
207. Cả hai đều được tạo bởi thư viện trình phân tích cú pháp HTML tích hợp của Python và giải pháp là

Loại hành vi không mong muốn phổ biến nhất là bạn không thể tìm thấy thẻ mà bạn biết là có trong tài liệu. Bạn đã thấy nó đi vào, nhưng

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
042 trả về
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
209 hoặc
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
53 trả về
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
002. Đây là một vấn đề phổ biến khác với trình phân tích cú pháp HTML tích hợp của Python, đôi khi bỏ qua các thẻ mà nó không hiểu. Một lần nữa, giải pháp tốt nhất là

Sự cố phiên bản không khớp

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    212 [trên đường dây
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    213]. Nguyên nhân do chạy phiên bản Python 2 cũ của Beautiful Soup trong Python 3 mà không chuyển đổi mã

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    214 - Nguyên nhân do chạy phiên bản Beautiful Soup Python 2 cũ trong Python 3

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    215 - Nguyên nhân do chạy phiên bản Beautiful Soup Python 3 trong Python 2

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    216 - Nguyên nhân do chạy mã Beautiful Soup 3 trên hệ thống chưa cài đặt BS3. Hoặc do viết mã Beautiful Soup 4 mà không biết tên gói đã đổi thành
    soup.title
    # The Dormouse's story
    
    soup.title.name
    # u'title'
    
    soup.title.string
    # u'The Dormouse's story'
    
    soup.title.parent.name
    # u'head'
    
    soup.p
    # 

    The Dormouse's story

    soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
    19

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    218 - Nguyên nhân do chạy mã Beautiful Soup 4 trên hệ thống chưa cài đặt BS4

Phân tích cú pháp XML

Theo mặc định, Beautiful Soup phân tích tài liệu dưới dạng HTML. Để phân tích một tài liệu dưới dạng XML, hãy chuyển “xml” làm đối số thứ hai cho hàm tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
01

Bạn sẽ cần phải

Các vấn đề về trình phân tích cú pháp khác

  • Nếu tập lệnh của bạn hoạt động trên một máy tính nhưng không hoạt động trên một máy tính khác hoặc trong một môi trường ảo nhưng không hoạt động trên một môi trường ảo khác hoặc bên ngoài môi trường ảo nhưng không hoạt động bên trong thì có thể là do hai môi trường có sẵn các thư viện trình phân tích cú pháp khác nhau. Ví dụ: bạn có thể đã phát triển tập lệnh trên máy tính đã cài đặt lxml, sau đó thử chạy tập lệnh đó trên máy tính chỉ cài đặt html5lib. Xem lý do tại sao điều này lại quan trọng và khắc phục sự cố bằng cách đề cập đến thư viện trình phân tích cú pháp cụ thể trong hàm tạo

    soup.title
    # The Dormouse's story
    
    soup.title.name
    # u'title'
    
    soup.title.string
    # u'The Dormouse's story'
    
    soup.title.parent.name
    # u'head'
    
    soup.p
    # 

    The Dormouse's story

    soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
    06

  • Because , all three HTML parsers convert tag and attribute names to lowercase. That is, the markup is converted to . If you want to preserve mixed-case or uppercase tags and attributes, you’ll need to

Điều khoản khác

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    221 [hoặc gần như bất kỳ
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    222 nào khác] - Vấn đề này xuất hiện trong hai tình huống chính. Đầu tiên, khi bạn cố gắng in một ký tự Unicode mà bảng điều khiển của bạn không biết cách hiển thị. [Xem trang này trên wiki Python để được trợ giúp. ] Thứ hai, khi bạn đang ghi vào một tệp và bạn chuyển vào một ký tự Unicode không được mã hóa mặc định của bạn hỗ trợ. Trong trường hợp này, giải pháp đơn giản nhất là mã hóa rõ ràng chuỗi Unicode thành UTF-8 bằng
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    223

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    224 - Nguyên nhân do truy cập vào
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    225 khi thẻ được đề cập không xác định thuộc tính
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    226. Các lỗi phổ biến nhất là
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    227 và
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    228. Sử dụng
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    229 nếu bạn không chắc chắn về định nghĩa của
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    226, giống như cách bạn làm với từ điển Python

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    231 - Điều này thường xảy ra vì bạn mong đợi
    mysql> select 'test\'s' as test_string;
    +-------------+
    | test_string |
    +-------------+
    | test's      |
    +-------------+
    042 trả về một thẻ hoặc chuỗi. Nhưng
    mysql> select 'test\'s' as test_string;
    +-------------+
    | test_string |
    +-------------+
    | test's      |
    +-------------+
    042 trả về _list_ gồm các thẻ và chuỗi–một đối tượng
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    234. Bạn cần lặp lại danh sách và xem
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    235 của từng cái. Hoặc, nếu bạn thực sự chỉ muốn một kết quả, bạn cần sử dụng
    soup.title
    # The Dormouse's story
    
    soup.title.name
    # u'title'
    
    soup.title.string
    # u'The Dormouse's story'
    
    soup.title.parent.name
    # u'head'
    
    soup.p
    # 

    The Dormouse's story

    soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
    53 thay vì
    mysql> select 'test\'s' as test_string;
    +-------------+
    | test_string |
    +-------------+
    | test's      |
    +-------------+
    042

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    238 - Điều này thường xảy ra vì bạn đã gọi cho
    soup.title
    # The Dormouse's story
    
    soup.title.name
    # u'title'
    
    soup.title.string
    # u'The Dormouse's story'
    
    soup.title.parent.name
    # u'head'
    
    soup.p
    # 

    The Dormouse's story

    soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
    53 và sau đó cố truy cập vào. thuộc tính foo` của kết quả. Nhưng trong trường hợp của bạn,
    soup.title
    # The Dormouse's story
    
    soup.title.name
    # u'title'
    
    soup.title.string
    # u'The Dormouse's story'
    
    soup.title.parent.name
    # u'head'
    
    soup.p
    # 

    The Dormouse's story

    soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
    53 không tìm thấy gì, vì vậy nó trả về
    mysql> select 'test\'s' as test_string;
    +-------------+
    | test_string |
    +-------------+
    | test's      |
    +-------------+
    002, thay vì trả về một thẻ hoặc một chuỗi. Bạn cần tìm ra lý do tại sao cuộc gọi
    soup.title
    # The Dormouse's story
    
    soup.title.name
    # u'title'
    
    soup.title.string
    # u'The Dormouse's story'
    
    soup.title.parent.name
    # u'head'
    
    soup.p
    # 

    The Dormouse's story

    soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
    53 của bạn không trả lại bất cứ điều gì

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    243 - Điều này thường xảy ra vì bạn đang coi một chuỗi như thể nó là một thẻ. Bạn có thể đang lặp lại một danh sách, hy vọng rằng nó không chứa gì ngoài các thẻ, trong khi nó thực sự chứa cả thẻ và chuỗi

Cải thiện hiệu suất

Beautiful Soup sẽ không bao giờ nhanh bằng trình phân tích cú pháp mà nó nằm trên. Nếu thời gian phản hồi là quan trọng, nếu bạn đang trả tiền cho thời gian sử dụng máy tính theo giờ hoặc nếu có bất kỳ lý do nào khác khiến thời gian sử dụng máy tính có giá trị hơn thời gian của lập trình viên, thì bạn nên quên Beautiful Soup đi và làm việc trực tiếp trên lxml

Điều đó nói rằng, có những điều bạn có thể làm để tăng tốc Beautiful Soup. Nếu bạn không sử dụng lxml làm trình phân tích cú pháp cơ bản, lời khuyên của tôi là. Beautiful Soup phân tích tài liệu nhanh hơn đáng kể bằng cách sử dụng lxml so với sử dụng html. trình phân tích cú pháp hoặc html5lib

Bạn có thể tăng tốc độ phát hiện mã hóa đáng kể bằng cách cài đặt thư viện cchardet

sẽ không giúp bạn tiết kiệm nhiều thời gian phân tích cú pháp tài liệu, nhưng nó có thể tiết kiệm rất nhiều bộ nhớ và giúp tìm kiếm tài liệu nhanh hơn nhiều

Dịch tài liệu này

Bản dịch mới của tài liệu Beautiful Soup được đánh giá rất cao. Các bản dịch phải được cấp phép theo giấy phép MIT, giống như Beautiful Soup và tài liệu tiếng Anh của nó là

Có hai cách để đưa bản dịch của bạn vào cơ sở mã chính và lên trang web Beautiful Soup

  1. Tạo một nhánh của kho lưu trữ Beautiful Soup, thêm bản dịch của bạn và đề xuất hợp nhất với nhánh chính, giống như bạn sẽ làm với một thay đổi được đề xuất đối với mã nguồn

  2. Gửi tin nhắn đến nhóm thảo luận Beautiful Soup với liên kết đến bản dịch của bạn hoặc đính kèm bản dịch của bạn vào tin nhắn

Sử dụng bản dịch tiếng Bồ Đào Nha của Trung Quốc hoặc Brazil làm mô hình của bạn. Đặc biệt, vui lòng dịch tệp nguồn

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
244, thay vì phiên bản HTML của tài liệu. Điều này cho phép xuất bản tài liệu ở nhiều định dạng khác nhau, không chỉ HTML

Súp Đẹp 3

Beautiful Soup 3 là sê-ri phát hành trước đó và không còn được phát triển tích cực nữa. Nó hiện được đóng gói với tất cả các bản phân phối Linux chính

$ apt-get cài đặt python-beansoup

Nó cũng được xuất bản thông qua PyPi với tên

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06

$ easy_install BeautifulSoup

$ pip cài đặt BeautifulSoup

Bạn cũng có thể tải xuống tarball của Beautiful Soup 3. 2. 0

Nếu bạn đã chạy

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
246 hoặc
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
247, nhưng mã của bạn không hoạt động, bạn đã cài đặt nhầm Beautiful Soup 3. Bạn cần chạy
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
248

Tài liệu về Beautiful Soup 3 được lưu trữ trực tuyến

Chuyển mã sang BS4

Hầu hết các mã được viết cho Beautiful Soup 3 sẽ hoạt động với Beautiful Soup 4 với một thay đổi đơn giản. Tất cả những gì bạn phải làm là thay đổi tên gói từ

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 thành
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
19. Vì vậy, điều này

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
02

trở thành cái này

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
03

  • Nếu bạn nhận được thông báo

    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    251 “Không có mô-đun nào có tên là BeautifulSoup”, vấn đề là bạn đang cố chạy mã Beautiful Soup 3, nhưng bạn chỉ cài đặt Beautiful Soup 4

  • Nếu bạn nhận được thông báo "Không có mô-đun có tên bs4" của

    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    251, vấn đề là bạn đang cố chạy mã Beautiful Soup 4, nhưng bạn chỉ cài đặt Beautiful Soup 3

Mặc dù BS4 hầu như tương thích ngược với BS3, nhưng hầu hết các phương pháp của nó đã không còn được dùng nữa và được đặt tên mới cho việc tuân thủ PEP 8. Có rất nhiều lần đổi tên và thay đổi khác, và một vài trong số chúng phá vỡ khả năng tương thích ngược

Đây là những gì bạn cần biết để chuyển đổi mã BS3 và thói quen của mình sang BS4

Bạn cần một trình phân tích cú pháp

Beautiful Soup 3 đã sử dụng

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
253 của Python, một mô-đun không được dùng nữa và đã bị xóa trong Python 3. 0. Beautiful Soup 4 sử dụng
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
167 theo mặc định, nhưng bạn có thể cắm lxml hoặc html5lib và sử dụng thay thế. Xem để so sánh

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
167 không phải là trình phân tích cú pháp giống như
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
253, nên bạn có thể thấy rằng Beautiful Soup 4 cung cấp cho bạn một cây phân tích cú pháp khác với Beautiful Soup 3 cho cùng một mã đánh dấu. Nếu bạn hoán đổi
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
167 lấy lxml hoặc html5lib, bạn có thể thấy rằng cây phân tích lại thay đổi. Nếu điều này xảy ra, bạn sẽ cần cập nhật mã cạo của mình để xử lý cây mới

Tên phương thức

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    258 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    259

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    260 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    261

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    262 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    263

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    264 ->
    mysql> select 'test\'s' as test_string;
    +-------------+
    | test_string |
    +-------------+
    | test's      |
    +-------------+
    114

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    266 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    267

  • ________ 3268 -> ________ 3269

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    270 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    271

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    272 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    273

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    274 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    275

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    276 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    277

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    278 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    279

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    280 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    281

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    282 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    283

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    284 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    285

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    286 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    287

  • ________ 3288 -> ________ 3289

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    290 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    291

Một số đối số cho hàm tạo Beautiful Soup đã được đổi tên vì những lý do tương tự

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    292 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    293

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    294 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    295

Tôi đã đổi tên một phương thức để tương thích với Python 3

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    296 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    297

Tôi đã đổi tên một thuộc tính để sử dụng thuật ngữ chính xác hơn

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    298 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    299

Tôi đã đổi tên ba thuộc tính để tránh sử dụng các từ có ý nghĩa đặc biệt đối với Python. Không giống như những thay đổi khác, những thay đổi này không tương thích ngược. Nếu bạn đã sử dụng các thuộc tính này trong BS3, mã của bạn sẽ bị hỏng trên BS4 cho đến khi bạn thay đổi chúng

  • ________ 3900 -> ________ 3901

  • ________ 3902 -> ________ 3903

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    904 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    905

Các phương thức này còn sót lại từ API Beautiful Soup 2. Chúng không còn được dùng nữa từ năm 2006 và hoàn toàn không nên sử dụng

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    906

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    907

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    908

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    907

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    910

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    911

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    912

máy phát điện

Tôi đã đặt các tên tuân thủ PEP 8 của trình tạo và biến chúng thành các thuộc tính

  • ________ 3913 -> ________ 3914

  • ________ 3915 -> ________ 3916

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    917 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    918

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    919 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    920

  • ________ 3921 -> ________ 3922

  • ________ 3923 -> ________ 3924

  • mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    925 ->
    mysql> create table escape_characters_demo[
           id int,
           string varchar[255]
           ];
    926

Vì vậy, thay vì điều này

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
04

bạn có thể viết cái này

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
05

[Nhưng mã cũ sẽ vẫn hoạt động. ]

Một số máy phát điện từng tạo ra _______1002 sau khi hoàn thành và sau đó dừng lại. đó là một lỗi. Bây giờ các máy phát điện chỉ dừng lại

Có hai máy phát điện mới,.

mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
003 mang lại các đối tượng NavigableString và
mysql> select 'test\'s' as test_string;
+-------------+
| test_string |
+-------------+
| test's      |
+-------------+
006 mang lại các chuỗi Python đã bị xóa khoảng trắng

XML

Không còn lớp

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
930 để phân tích cú pháp XML. Để phân tích cú pháp XML, bạn chuyển vào “xml” làm đối số thứ hai cho hàm tạo
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06. Vì lý do tương tự, hàm tạo
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 không còn nhận ra đối số
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
933

Việc xử lý các thẻ XML phần tử trống của Beautiful Soup đã được cải thiện. Trước đây khi bạn phân tích cú pháp XML, bạn phải nói rõ ràng thẻ nào được coi là thẻ phần tử rỗng. Đối số

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
934 cho hàm tạo không còn được nhận dạng. Thay vào đó, Beautifulsoup coi thẻ trống là thẻ phần tử trống. Nếu bạn thêm một phần tử con vào thẻ phần tử trống, nó sẽ không còn là thẻ phần tử trống nữa

thực thể

Một thực thể HTML hoặc XML đến luôn được chuyển đổi thành ký tự Unicode tương ứng. Beautiful Soup 3 có một số cách đối phó với các thực thể chồng chéo, đã bị loại bỏ. Hàm tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 không còn nhận ra các đối số
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
936 hoặc
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
937. [ vẫn có
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
938, nhưng mặc định của nó bây giờ là biến các trích dẫn thông minh thành Unicode. ] Các hằng số
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
939,
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
940 và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
941 đã bị xóa vì chúng định cấu hình một tính năng [chuyển đổi một số chứ không phải tất cả các thực thể thành ký tự Unicode] không còn tồn tại

Nếu bạn muốn biến các ký tự Unicode trở lại thành các thực thể HTML trên đầu ra, thay vì biến chúng thành các ký tự UTF-8, bạn cần sử dụng một

Điều khoản khác

bây giờ hoạt động đệ quy. Nếu thẻ A chỉ chứa một thẻ B và không có gì khác, thì A. chuỗi giống như B. chuỗi. [Trước đây, nó là Không có. ]

như

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
35 có danh sách các chuỗi làm giá trị của chúng, không phải chuỗi. Điều này có thể ảnh hưởng đến cách bạn tìm kiếm theo lớp CSS

Các đối tượng

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 hiện triển khai phương thức
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
944, sao cho hai đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 được coi là bằng nhau nếu chúng tạo ra cùng một đánh dấu. Điều này có thể thay đổi hành vi của tập lệnh nếu bạn đặt các đối tượng
soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
26 vào từ điển hoặc đặt

Nếu bạn vượt qua một trong các phương thức

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
947 và một đối số dành riêng cho thẻ như , Beautiful Soup sẽ tìm kiếm các thẻ phù hợp với tiêu chí dành riêng cho thẻ của bạn và thẻ phù hợp với giá trị của bạn cho. Nó sẽ không tự tìm thấy các chuỗi. Trước đây, Beautiful Soup đã bỏ qua các đối số dành riêng cho thẻ và tìm kiếm các chuỗi

Hàm tạo

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all['a'] # [Elsie, # Lacie, # Tillie] soup.find[id="link3"] # Tillie
06 không còn nhận ra đối số đánh dấuMassage. Giờ đây, trách nhiệm của trình phân tích cú pháp là xử lý đánh dấu một cách chính xác

Các lớp trình phân tích cú pháp thay thế hiếm khi được sử dụng như

mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
949 và
mysql> create table escape_characters_demo[
       id int,
       string varchar[255]
       ];
950 đã bị xóa. Bây giờ là quyết định của trình phân tích cú pháp về cách xử lý đánh dấu mơ hồ

Làm cách nào để xử lý các ký tự đặc biệt trong truy vấn MySQL?

MySQL - Cách đưa các ký tự đặc biệt vào truy vấn .
\0 - Ký tự ASCII NUL [0x00]
\' - Một ký tự nháy đơn [ ' ]
\" - Ký tự trích dẫn kép [ " ]
\b - Một ký tự xóa lùi
\n - Một ký tự xuống dòng [linefeed]
\r - Ký tự xuống dòng
\t - Một ký tự tab
\Z - ASCII 26 [Control-Z]

Những ký tự đặc biệt nào không được phép trong MySQL?

1 câu trả lời .
ASCII. U+0001. U+007F
Mở rộng. U+0080. U+FFFF

VARCHAR có cho phép các ký tự đặc biệt trong MySQL không?

Cột VARCHAR, đúng như tên gọi, lưu trữ dữ liệu có độ dài thay đổi. Chúng có thể lưu trữ các ký tự, số và ký tự đặc biệt giống như cột CHAR và có thể hỗ trợ các chuỗi có kích thước lên tới 8000 byte.

Làm cách nào để thoát các ký tự đặc biệt trong mật khẩu MySQL?

Sử dụng dấu gạch chéo ngược để "thoát" ký hiệu đô la trong mật khẩu . "\$****"

Chủ Đề