Hướng dẫn which module can you use to parse an xml file using python? - bạn có thể sử dụng mô-đun nào để phân tích tệp xml bằng python?

Bài viết này tập trung vào cách người ta có thể phân tích một tệp XML đã cho và trích xuất một số dữ liệu hữu ích ra khỏi nó theo cách có cấu trúc.

XML: XML là viết tắt của ngôn ngữ đánh dấu mở rộng. Nó được thiết kế để lưu trữ và vận chuyển dữ liệu. Nó được thiết kế để có thể đọc được cả con người và máy. Đó là lý do tại sao, các mục tiêu thiết kế của XML nhấn mạnh tính đơn giản, tính tổng quát và khả năng sử dụng trên internet. Tệp XML được phân tích cú pháp trong hướng dẫn này thực sự là nguồn cấp dữ liệu RSS. XML stands for eXtensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable.That’s why, the design goals of XML emphasize simplicity, generality, and usability across the Internet.
The XML file to be parsed in this tutorial is actually a RSS feed.

RSS: RSS (Tóm tắt trang web phong phú, thường được gọi là cung cấp thực sự đơn giản) sử dụng một gia đình các định dạng nguồn cấp dữ liệu web tiêu chuẩn để xuất bản các mục blog thông tin giống như thông tin thường xuyên, tiêu đề tin tức, âm thanh, video. RSS là văn bản đơn giản được định dạng XML.RSS(Rich Site Summary, often called Really Simple Syndication) uses a family of standard web feed formats to publish frequently updated informationlike blog entries, news headlines, audio, video. RSS is XML formatted plain text.

  • Bản thân định dạng RSS tương đối dễ đọc cả bởi các quy trình tự động và cả con người.
  • RSS được xử lý trong hướng dẫn này là nguồn cấp dữ liệu RSS của những câu chuyện tin tức hàng đầu từ một trang web tin tức nổi tiếng. Bạn có thể kiểm tra xem nó ra ở đây. Mục tiêu của chúng tôi là xử lý nguồn cấp dữ liệu RSS này (hoặc tệp XML) và lưu nó ở một số định dạng khác để sử dụng trong tương lai.

Mô -đun Python được sử dụng: Bài viết này sẽ tập trung vào việc sử dụng mô -đun XML sẵn có trong Python để phân tích cú pháp XML và trọng tâm chính sẽ là API XML ElementTree của mô -đun này. This article will focus on using inbuilt xml module in python for parsing XML and the main focus will be on the ElementTree XML API of this module.

Implementation:

import

tree = ET.parse(xmlfile)
0

import

tree = ET.parse(xmlfile)
2

import

tree = ET.parse(xmlfile)
4

tree = ET.parse(xmlfile)
5
tree = ET.parse(xmlfile)
6

tree = ET.parse(xmlfile)
7
tree = ET.parse(xmlfile)
8
tree = ET.parse(xmlfile)
9
root = tree.getroot()
0

tree = ET.parse(xmlfile)
7
root = tree.getroot()
2
root = tree.getroot()
3
root = tree.getroot()
4
root = tree.getroot()
5
root = tree.getroot()
6
root = tree.getroot()
7
root = tree.getroot()
8

root = tree.getroot()
9
for item in root.findall('./channel/item'):
0

tree = ET.parse(xmlfile)
5
for item in root.findall('./channel/item'):
2

tree = ET.parse(xmlfile)
7
for item in root.findall('./channel/item'):
4
tree = ET.parse(xmlfile)
9
for item in root.findall('./channel/item'):
6

tree = ET.parse(xmlfile)
7
for item in root.findall('./channel/item'):
8
tree = ET.parse(xmlfile)
9
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
0

tree = ET.parse(xmlfile)
7
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
2
tree = ET.parse(xmlfile)
9
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
4

tree = ET.parse(xmlfile)
7
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
6
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
7
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
8
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
9
for child in item:
0
for child in item:
1

root = tree.getroot()
9
for child in item:
3
tree = ET.parse(xmlfile)
9
for child in item:
5

root = tree.getroot()
9
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
6
for child in item:
8
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
8
if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
0

Các

if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
9
news[child.tag] = child.text.encode('utf8')
0
news[child.tag] = child.text.encode('utf8')
1

if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
1
news[child.tag] = child.text.encode('utf8')
3
tree = ET.parse(xmlfile)
9
news[child.tag] = child.text.encode('utf8')
5
news[child.tag] = child.text.encode('utf8')
6
news[child.tag] = child.text.encode('utf8')
7

root = tree.getroot()
9
news[child.tag] = child.text.encode('utf8')
9

tree = ET.parse(xmlfile)
7
{'description': 'Ignis has a tough competition already, from Hyun.... ,
 'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
 'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
 'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }
1
{'description': 'Ignis has a tough competition already, from Hyun.... ,
 'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
 'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
 'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }
2

tree = ET.parse(xmlfile)
5
{'description': 'Ignis has a tough competition already, from Hyun.... ,
 'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
 'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
 'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }
4

tree = ET.parse(xmlfile)
7
{'description': 'Ignis has a tough competition already, from Hyun.... ,
 'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
 'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
 'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }
6
tree = ET.parse(xmlfile)
9
{'description': 'Ignis has a tough competition already, from Hyun.... ,
 'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
 'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
 'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }
8
{'description': 'Ignis has a tough competition already, from Hyun.... ,
 'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
 'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
 'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
 'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }
9
root = tree.getroot()
6import1
root = tree.getroot()
6import3
root = tree.getroot()
6import5
root = tree.getroot()
6____979

tree = ET.parse(xmlfile)
7
root = tree.getroot()
2
root = tree.getroot()
3
tree = ET.parse(xmlfile)
04
tree = ET.parse(xmlfile)
05
tree = ET.parse(xmlfile)
06

root = tree.getroot()
9
tree = ET.parse(xmlfile)
08
tree = ET.parse(xmlfile)
9
tree = ET.parse(xmlfile)
10
tree = ET.parse(xmlfile)
9
tree = ET.parse(xmlfile)
12

root = tree.getroot()
9
tree = ET.parse(xmlfile)
14

root = tree.getroot()
9
tree = ET.parse(xmlfile)
16

tree = ET.parse(xmlfile)
5
tree = ET.parse(xmlfile)
18

tree = ET.parse(xmlfile)
7
tree = ET.parse(xmlfile)
20

tree = ET.parse(xmlfile)
7
for item in root.findall('./channel/item'):

        # empty news dictionary
        news = {}

        # iterate child elements of item
        for child in item:

            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.text.encode('utf8')

        # append news dictionary to news items list
        newsitems.append(news)
2
tree = ET.parse(xmlfile)
9
tree = ET.parse(xmlfile)
24
root = tree.getroot()
5
news[child.tag] = child.text.encode('utf8')
7

tree = ET.parse(xmlfile)
7
tree = ET.parse(xmlfile)
28
tree = ET.parse(xmlfile)
29
news[child.tag] = child.text.encode('utf8')
7

tree = ET.parse(xmlfile)
31
tree = ET.parse(xmlfile)
32
tree = ET.parse(xmlfile)
9
tree = ET.parse(xmlfile)
9
tree = ET.parse(xmlfile)
35
news[child.tag] = child.text.encode('utf8')
1

tree = ET.parse(xmlfile)
7
tree = ET.parse(xmlfile)
38

Mã trên sẽ:

  • Tải nguồn cấp RSS từ URL được chỉ định và lưu nó dưới dạng tệp XML.
  • Phân tích tệp XML để lưu tin tức dưới dạng danh sách các từ điển trong đó mỗi từ điển là một mục tin tức duy nhất.
  • Lưu các mục tin tức vào một tệp CSV.

Hãy để chúng tôi cố gắng hiểu mã theo từng mảnh:

  • Đang tải và tiết kiệm RSS Feed
    def loadRSS():
        # url of rss feed
        url = 'http://www.hindustantimes.com/rss/topnews/rssfeed.xml'
        # creating HTTP response object from given url
        resp = requests.get(url)
        # saving the xml file
        with open('topnewsfeed.xml', 'wb') as f:
            f.write(resp.content)
    def loadRSS():
        # url of rss feed
        url = 'http://www.hindustantimes.com/rss/topnews/rssfeed.xml'
        # creating HTTP response object from given url
        resp = requests.get(url)
        # saving the xml file
        with open('topnewsfeed.xml', 'wb') as f:
            f.write(resp.content)

    Ở đây, trước tiên chúng tôi đã tạo một đối tượng phản hồi HTTP bằng cách gửi yêu cầu HTTP đến URL của nguồn cấp RSS. Nội dung phản hồi hiện chứa dữ liệu tệp XML mà chúng tôi lưu dưới dạng topnewsfeed.xml trong thư mục cục bộ của chúng tôi. Để biết thêm về cách thức các mô -đun yêu cầu hoạt động, hãy theo dõi bài viết này: Nhận và đăng yêu cầu bằng Pythontopnewsfeed.xml in our local directory.
    For more insight on how requests module works, follow this article:
    GET and POST requests using Python

  • Phân tích cú pháp xmlwe đã tạo hàm parsexml () thành tệp xml phân tích. Chúng tôi biết rằng XML là một định dạng dữ liệu phân cấp vốn có và cách tự nhiên nhất để thể hiện nó là với một cây. Nhìn vào hình ảnh bên dưới ví dụ: Ở đây, chúng tôi đang sử dụng mô -đun xml.etree.elementtree (gọi nó là ET, tóm lại). Tree phần tử có hai lớp cho mục đích này - ElementTree đại diện cho toàn bộ XMLDocument dưới dạng cây và phần tử đại diện cho một nút duy nhất trong cây này. Các tương tác với toàn bộ tài liệu (đọc và ghi vào/từ các tệp) thường được thực hiện ở cấp độ ElementTree. Các tương tác với một phần tử XML duy nhất và các phần tử phụ của nó được thực hiện ở cấp phần tử.
    We have created parseXML() function to parse XML file. We know that XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. Look at the image below for example:
    Hướng dẫn which module can you use to parse an xml file using python? - bạn có thể sử dụng mô-đun nào để phân tích tệp xml bằng python?

    Here, we are using xml.etree.ElementTree (call it ET, in short) module. Element Tree has two classes for this purpose – ElementTree represents the whole XML
    document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.

    OK, vì vậy hãy để Lừa đi qua hàm parsexml () ngay bây giờ:parseXML() function now:

    tree = ET.parse(xmlfile)

    Ở đây, chúng tôi tạo một đối tượng ElementTree bằng cách phân tích XMLFile đã qua.ElementTree object by parsing the passed xmlfile.

    root = tree.getroot()

    Hàm getRoot () Trả về gốc của cây dưới dạng đối tượng phần tử.function return the root of tree as an Element object.

    for item in root.findall('./channel/item'):

    Bây giờ, một khi bạn đã xem cấu trúc của tệp XML của mình, bạn sẽ nhận thấy rằng chúng tôi chỉ quan tâm đến phần tử mục ../ Kênh/Mục thực sự là XPath Syntax (XPath là ngôn ngữ để giải quyết các phần của tài liệu XML) . Ở đây, chúng tôi muốn tìm tất cả các cháu gái của trẻ em Channel of the Root (được ký hiệu là ‘.item element.
    ./channel/item is actually XPath syntax (XPath is a language for addressing parts of an XML document). Here, we want to find all item grand-children of channel children of the root(denoted by ‘.’) element.
    You can read more about supported XPath syntax here.

    for item in root.findall('./channel/item'):
    
            # empty news dictionary
            news = {}
    
            # iterate child elements of item
            for child in item:
    
                # special checking for namespace object content:media
                if child.tag == '{http://search.yahoo.com/mrss/}content':
                    news['media'] = child.attrib['url']
                else:
                    news[child.tag] = child.text.encode('utf8')
    
            # append news dictionary to news items list
            newsitems.append(news)

    Bây giờ, chúng tôi biết rằng chúng tôi đang lặp đi lặp lại thông qua các yếu tố vật phẩm trong đó mỗi phần tử mục chứa một tin tức. Vì vậy, chúng tôi tạo ra một từ điển tin tức trống trong đó chúng tôi sẽ lưu trữ tất cả dữ liệu có sẵn về mục tin tức. Để lặp lại từng phần tử con của một yếu tố, chúng tôi chỉ đơn giản lặp đi lặp lại thông qua nó, như thế này:item elements where each item element contains one news. So, we create an empty news dictionary in which we will store all data available about news item. To iterate though each child element of an element, we simply iterate through it, like this:

    for child in item:

    Bây giờ, hãy chú ý một phần tử mục mẫu ở đây:

    Hướng dẫn which module can you use to parse an xml file using python? - bạn có thể sử dụng mô-đun nào để phân tích tệp xml bằng python?

    Chúng tôi sẽ phải xử lý các thẻ không gian tên một cách riêng biệt khi chúng được mở rộng đến giá trị ban đầu của chúng, khi được phân tích cú pháp. Vì vậy, chúng tôi làm một cái gì đó như thế này:

    if child.tag == '{http://search.yahoo.com/mrss/}content':
                    news['media'] = child.attrib['url']

    Child.Attrib là một từ điển của tất cả các thuộc tính liên quan đến một yếu tố. Ở đây, chúng tôi quan tâm đến thuộc tính URL của phương tiện truyền thông: Tag tên nội dung. Bây giờ, cho tất cả trẻ em khác, chúng tôi chỉ đơn giản là làm:is a dictionary of all the attributes related to an element. Here, we are interested in url attribute of media:content namespace tag.
    Now, for all other children, we simply do:

    news[child.tag] = child.text.encode('utf8')

    con.tag chứa tên của yếu tố con. con.TEXT lưu trữ tất cả các văn bản bên trong phần tử con đó. Vì vậy, cuối cùng, một phần tử mục mẫu được chuyển đổi thành từ điển và trông như thế này:contains the name of child element. child.text stores all the text inside that child element. So, finally, a sample item element is converted to a dictionary and looks like this:

    {'description': 'Ignis has a tough competition already, from Hyun.... ,
     'guid': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
     'link': 'http://www.hindustantimes.com/autos/maruti-ignis-launch.... ,
     'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/... ,
     'pubDate': 'Thu, 12 Jan 2017 12:33:04 GMT ',
     'title': 'Maruti Ignis launches on Jan 13: Five cars that threa..... }

    Sau đó, chúng tôi chỉ đơn giản là nối phần yếu tố dict này vào danh sách newsitems.finally, danh sách này được trả về.newsitems.
    Finally, this list is returned.

  • Lưu dữ liệu vào tệp CSV, chúng tôi chỉ cần lưu danh sách các mục tin tức vào tệp CSV để nó có thể được sử dụng hoặc sửa đổi dễ dàng trong tương lai bằng cách sử dụng hàm savetocsv (). Để biết thêm về việc viết các yếu tố từ điển vào tệp CSV, hãy xem qua bài viết này: Làm việc với các tệp CSV trong Python
    Now, we simply save the list of news items to a CSV file so that it could be used or modified easily in future using savetoCSV() function. To know more about writing dictionary elements to a CSV file, go through this article:
    Working with CSV files in Python

Vì vậy, bây giờ, đây là cách dữ liệu được định dạng của chúng tôi trông như thế nào:

Hướng dẫn which module can you use to parse an xml file using python? - bạn có thể sử dụng mô-đun nào để phân tích tệp xml bằng python?

Như bạn có thể thấy, dữ liệu tệp XML phân cấp đã được chuyển đổi thành tệp CSV đơn giản để tất cả các câu chuyện tin tức được lưu trữ dưới dạng bảng. Điều này giúp việc mở rộng cơ sở dữ liệu dễ dàng hơn. Ngoài ra, người ta có thể sử dụng dữ liệu giống JSON trực tiếp trong các ứng dụng của họ! Đây là giải pháp thay thế tốt nhất để trích xuất dữ liệu từ các trang web không cung cấp API công khai nhưng cung cấp một số nguồn cấp RSS.
Also, one can use the JSON-like data directly in their applications! This is the best alternative for extracting data from websites which do not provide a public API but provide some RSS feeds.

Tất cả các mã và tệp được sử dụng trong bài viết trên có thể được tìm thấy ở đây.

Tiếp theo là gì?

  • Bạn có thể xem xét thêm các nguồn cấp RSS của trang web tin tức được sử dụng trong ví dụ trên. Bạn có thể cố gắng tạo một phiên bản mở rộng của ví dụ trên bằng cách phân tích các nguồn cấp RSS khác.
  • Bạn có phải là một người hâm mộ cricket? Sau đó, nguồn cấp dữ liệu RSS này phải được bạn quan tâm! Bạn có thể phân tích tệp XML này để quét thông tin về các trận đấu cricket trực tiếp và sử dụng để tạo một thông báo máy tính để bàn!

Bài kiểm tra HTML và XML

Bài viết này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết và gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên


Mô -đun XML trong Python là gì?

Practical Data Science using Python XML stands for "Extensible Markup Language". It is mainly used in webpages, where the data has a specific structure. It has elements, defined by a beginning and an ending tag. A tag is a markup construct that begins with < and ends with >.

Phương pháp nào được sử dụng để phân tích tài liệu XML?

DOM và SAX là hai API tiêu chuẩn để xử lý các tài liệu XML. Hầu hết các trình phân tích cú pháp XML chính hỗ trợ họ. are the two standard APIs for processing XML documents. Most major XML parsers support them.

Làm thế nào tôi có thể phân tích XML?

XML DOM (mô hình đối tượng tài liệu) xác định các thuộc tính và phương thức để truy cập và chỉnh sửa XML.Tuy nhiên, trước khi có thể truy cập tài liệu XML, nó phải được tải vào đối tượng DOM XML.Tất cả các trình duyệt hiện đại có trình phân tích cú pháp XML tích hợp có thể chuyển đổi văn bản thành đối tượng XML DOM.All modern browsers have a built-in XML parser that can convert text into an XML DOM object.

Tôi có thể sử dụng súp đẹp để phân tích xml không?

Đẹp là một trong những thư viện được sử dụng nhiều nhất khi nói đến việc quét web với Python.Vì các tệp XML tương tự như các tệp HTML, nên nó cũng có khả năng phân tích chúng.Để phân tích các tệp XML bằng cách sử dụng đẹp, tốt nhất là bạn nên sử dụng trình phân tích cú pháp LXML của Python.it is also capable of parsing them. To parse XML files using BeautifulSoup though, it's best that you make use of Python's lxml parser.