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. Show 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. 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.
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:
tree = ET.parse(xmlfile)0
tree = ET.parse(xmlfile)2
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()6 import 1root = tree.getroot()6 import 3root = tree.getroot()6 import 5root = 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ẽ:
Hãy để chúng tôi cố gắng hiểu mã theo từng mảnh:
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: 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. 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à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 Phân tích cú pháp XML trong Python là gì?Python cho phép phân tích các tài liệu XML này bằng hai mô -đun là XML. Etree. Mô -đun ElementTree và Minidom (triển khai DOM tối thiểu). Phân tích cú pháp có nghĩa là đọc thông tin từ một tệp và chia nó thành các phần bằng cách xác định các phần của tệp XML cụ thể đó.to read information from a file and split it into pieces by identifying parts of that particular XML file.
Phân tích cú pháp trong XML với ví dụ là gì?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.. However, before an XML document can be accessed, it must be loaded into an XML DOM object. All modern browsers have a built-in XML parser that can convert text into an XML DOM object.
Ý bạn là gì khi phân tích cú pháp XML?Sự định nghĩa.XML Phân tích cú pháp là quá trình đọc tài liệu XML và cung cấp giao diện cho ứng dụng người dùng để truy cập tài liệu.Trình phân tích cú pháp XML là một thiết bị phần mềm hoàn thành các nhiệm vụ như vậy.the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.
Làm thế nào để bạn đọc và phân tích XML trong Python?Để đọc một tệp XML bằng ElementTree, trước tiên, chúng tôi nhập lớp ElementTree được tìm thấy bên trong thư viện XML, dưới tên ET (thông tin chung).Sau đó chuyển tên tệp của tệp XML cho ElementTree.Phương thức Parse (), để cho phép phân tích tệp XML của chúng tôi.Sau đó, nhận được gốc (thẻ cha) của tệp XML của chúng tôi bằng GetRoot (). |