Hướng dẫn can python read html files? - python có thể đọc tệp html không?

Điều kiện tiên quyết: Đẹp: Beautifulsoup

Phân tích phân tích có nghĩa là chia một tệp hoặc nhập thành các mẩu thông tin/dữ liệu có thể được lưu trữ cho mục đích sử dụng cá nhân của chúng tôi trong tương lai. Đôi khi, chúng tôi cần dữ liệu từ một tệp hiện có được lưu trữ trên máy tính của chúng tôi, kỹ thuật phân tích cú pháp có thể được sử dụng trong các trường hợp như vậy. Việc phân tích cú pháp bao gồm nhiều kỹ thuật được sử dụng để trích xuất dữ liệu từ một tệp. Sau đây bao gồm sửa đổi tệp, xóa một cái gì đó khỏi tệp, in dữ liệu, sử dụng phương thức trình tạo con đệ quy để di chuyển dữ liệu khỏi tệp, tìm trẻ của thẻ, quét web từ một liên kết để trích xuất thông tin hữu ích, v.v. means dividing a file or input into pieces of information/data that can be stored for our personal use in the future. Sometimes, we need data from an existing file stored on our computers, parsing technique can be used in such cases. The parsing includes multiple techniques used to extract data from a file. The following includes Modifying the file, Removing something from the file, Printing data, using the recursive child generator method to traverse data from the file, finding the children of tags, web scraping from a link to extract useful information, etc.

Sửa đổi tệp

Sử dụng phương thức Prettify để sửa đổi mã HTML từ- https://festive-knuth-1279a2.netlify.app/, trông đẹp hơn. Prettify làm cho mã nhìn ở dạng tiêu chuẩn giống như mã được sử dụng trong mã VS.prettify method to modify the HTML code from- https://festive-knuth-1279a2.netlify.app/, look better. Prettify makes the code look in the standard form like the one used in VS Code.

Example:

Python3

Output:

Hướng dẫn can python read html files? - python có thể đọc tệp html không?

Xóa một thẻ

Một thẻ có thể được xóa bằng cách sử dụng phương thức phân tách và phương thức select_one với các bộ chọn CSS để chọn và sau đó xóa phần tử thứ hai khỏi thẻ LI và sau đó sử dụng phương thức Prettify để sửa đổi mã HTML khỏi tệp index.html.

Example:

Tệp sử dụng:

Hướng dẫn can python read html files? - python có thể đọc tệp html không?

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import0= import2223bs4 1

import5

import6import7

Output:

Tìm thẻ

Thẻ có thể được tìm thấy bình thường và được in bình thường bằng print ().

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import6HTMLFile 9

import6=1

import6=1

import6=5

import6=7

Output:

Hướng dẫn can python read html files? - python có thể đọc tệp html không?

import0= import2223bs4 1

Tìm thẻrecursiveChildGenerator method is used to traverse tags, which recursively finds all the tags within tags from thefile.

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import0= import2223bs4 1

Tìm thẻ

"index.html"5import6"index.html"7

Output:

Thẻ có thể được tìm thấy bình thường và được in bình thường bằng print (). 

HTMLFile 3= bs4 7bs4 8bs4 1name attribute of the tag to print its name and the text attribute to print its text along with the code of the tag- ul from the file.

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import6bs4 09bs4 10bs4 1

Output:

import0= import2223bs4 1 

Tìm thẻChildren attribute is used to get the children of a tag. The Children attribute returns ‘tags with spaces’ between them, we’re adding a condition- e. name is not None to print only names of the tags from the file.

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import0= import2223bs4 1

Tìm thẻ

import6bs4 49

Output:

Thẻ có thể được tìm thấy bình thường và được in bình thường bằng print ().

HTMLFile 3= bs4 7bs4 8bs4 1Descendants attribute is used to get all the descendants (Children at all levels) of a tag from the file.

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import0= import2223bs4 1

Tìm thẻ

Thẻ có thể được tìm thấy bình thường và được in bình thường bằng print ().

Output:

HTMLFile 3= bs4 7bs4 8bs4 1 

Đi qua thẻ

Phương pháp RecursiveChildGenerator được sử dụng để đi qua các thẻ, trong đó tìm thấy tất cả các thẻ trong các thẻ từ Thefile.find_all method is used to find all the elements (name and text) inside the p tag from the file.

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import0= import2223bs4 1

"index.html"2import6bs4 09import17bs4 1

Output:

Tìm thẻ

Thẻ có thể được tìm thấy bình thường và được in bình thường bằng print ().select method to use the CSS selectors to find the second element from the li tag from the file.

Example:

Python3

from bs4 import BeautifulSoup

Các

bs4 2= bs4 4

bs4 5= bs4 7bs4 8bs4 1

import6import40import3import42

Output: