Trang web Goodreads có API này để truy cập 'kệ của người dùng:' //www.goodreads.com/review/list/20990068.xml?key=ngvcqaq6tn9w4hnpw8kquw&v=2&shelf=toread
Nó trả về XML. Tôi đang cố gắng tạo ra một dự án Django hiển thị sách trên kệ từ API này. Tôi đang tìm cách tìm hiểu làm thế nào (hoặc nếu có một cách tốt hơn) để viết quan điểm của tôi để tôi có thể chuyển một đối tượng cho mẫu của mình. Hiện tại, đây là những gì tôi đang làm:
import urllib2 def homepage(request): file = urllib2.urlopen('//www.goodreads.com/review/list/20990068.xml?key=nGvCqaQ6tn9w4HNpW8kquw&v=2&shelf=toread') data = file.read() file.close() dom = parseString(data)Tôi không hoàn toàn chắc chắn làm thế nào để thao túng đối tượng này nếu tôi thực hiện điều này một cách chính xác. Tôi đang làm theo hướng dẫn này.
Alecxe
450K114 Huy hiệu vàng1045 Huy hiệu bạc1168 Huy hiệu đồng114 gold badges1045 silver badges1168 bronze badges
Đã hỏi ngày 9 tháng 6 năm 2014 lúc 16:31Jun 9, 2014 at 16:31
1
Tôi sẽ sử dụng xmltodict để tạo từ điển Python ra khỏi cấu trúc dữ liệu XML và chuyển từ điển này đến mẫu bên trong bối cảnh:
import urllib2 import xmltodict def homepage(request): file = urllib2.urlopen('//www.goodreads.com/review/list/20990068.xml?key=nGvCqaQ6tn9w4HNpW8kquw&v=2&shelf=toread') data = file.read() file.close() data = xmltodict.parse(data) return render_to_response('my_template.html', {'data': data})Đã trả lời ngày 9 tháng 6 năm 2014 lúc 16:38Jun 9, 2014 at 16:38
Alecxealecxealecxe
450K114 Huy hiệu vàng1045 Huy hiệu bạc1168 Huy hiệu đồng114 gold badges1045 silver badges1168 bronze badges
0
Đã hỏi ngày 9 tháng 6 năm 2014 lúc 16:31
import traceback import urllib3 import xmltodict def getxml(): url = "//yoursite/your.xml" http = urllib3.PoolManager() response = http.request('GET', url) try: data = xmltodict.parse(response.data) except: print("Failed to parse xml from response (%s)" % traceback.format_exc()) return data
Tôi sẽ sử dụng xmltodict để tạo từ điển Python ra khỏi cấu trúc dữ liệu XML và chuyển từ điển này đến mẫu bên trong bối cảnh:
Đã trả lời ngày 9 tháng 6 năm 2014 lúc 16:383 gold badges26 silver badges42 bronze badges
AlecxealecxeSep 18, 2019 at 10:01
gies0rgies0rgies0r
xmltodict Sử dụng urllib31 gold badge35 silver badges46 bronze badges
Josh Correia
import requests import xmltodict url = "//yoursite/your.xml" response = requests.get(url) data = xmltodict.parse(response.content)3.1663 huy hiệu vàng26 Huy hiệu bạc42 Huy hiệu đồngMay 25, 2020 at 14:09
Đã trả lời ngày 18 tháng 9 năm 2019 lúc 10:01Vincent
4.1351 Huy hiệu vàng35 Huy hiệu bạc46 Huy hiệu đồng7 silver badges6 bronze badges
Trong Python, bạn có thể sử dụng Urllib.Request và xml.etree.elementtree thư viện để phân tích và đọc XML từ URL. Sau đây là một ví dụ:urllib.request and xml.etree.ElementTree library to parse and read XML from URL. The following is an example: Dưới đây chương trình Python sẽ tải xuống và đọc nguồn cấp dữ liệu RSS của Oracle thông qua URL. Nó sẽ mở URL (//blogs.oracle.com/oraclepartners/database-7/rss) bằng phương thức URLOPEN của thư viện Urllib.Request và sau đó nó sẽ phân tích và đọc dữ liệu XML bằng phương pháp phân tích thư viện xml. Etree.elementtree.urlopen method of urllib.request library and then it will parse and read the XML data using parse method of library xml.etree.ElementTree.Ví dụ chương trình Python - Đọc XML từ URL
Đầu ra
Webcast: Oracle Database 19c: Strategy, Features & New Customers – April 9
Mon, 01 Apr 2019 13:09:04 +0000
//blogs.oracle.com/oraclepartners/webcast%3A-oracle-database-19c%3A-strategy%2C-features-new-customers-%E2%80%93-april-9
Win Over Financial Services Prospects with MySQL Enterprise Edition
Thu, 28 Mar 2019 21:10:44 +0000
//blogs.oracle.com/oraclepartners/win-over-financial-services-prospects-with-mysql-enterprise-edition
How will you design the future for Data & Analytics? - April 26, 2019
Thu, 21 Mar 2019 12:38:22 +0000
//blogs.oracle.com/oraclepartners/how-will-you-design-the-future-for-data-analytics-april-26%2C-2019
MySQL Enterprise Edition - High Availablity Campaign Now Available
Thu, 07 Mar 2019 23:00:00 +0000
//blogs.oracle.com/oraclepartners/mysql-enterprise-edition-high-availablity-campaign-now-available
Stay Ahead of the Game with Autonomous Database Training
....Xem thêm:
Bạn nghĩ sao?
Xem thêm
- Bài viết trước không Install Oracle Apex 19.1 in Eleven Easy Steps
- Bài viết tiếp theo để cài đặt PIP trên Windows 10? How to Install PIP on Windows 10?