Hướng dẫn python get xml from url - python lấy xml từ url

Trang web Goodreads có API này để truy cập 'kệ của người dùng:' https://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('https://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.

Hướng dẫn python get xml from url - python lấy xml từ url

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('https://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

Hướng dẫn python get xml from url - python lấy xml từ url

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 = "https://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 = "https://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

Hướng dẫn python get xml from url - python lấy xml từ url

Đã 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:

Ví dụ chương trình Python - Đọc XML từ URL

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 (https://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.

from urllib.request import urlopen
from xml.etree.ElementTree import parse

var_url = urlopen('https://blogs.oracle.com/oraclepartners/database-7/rss')
xmldoc = parse(var_url)

for item in xmldoc.iterfind('channel/item'):
    title = item.findtext('title')
    date = item.findtext('pubDate')
    link = item.findtext('link')

    print(title)
    print(date)
    print(link)
    print()

Đầu ra

Webcast: Oracle Database 19c: Strategy, Features & New Customers – April 9
Mon, 01 Apr 2019 13:09:04 +0000
https://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
https://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
https://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
https://blogs.oracle.com/oraclepartners/mysql-enterprise-edition-high-availablity-campaign-now-available

Stay Ahead of the Game with Autonomous Database Training
....

Xem thêm:

  • Ví dụ về ghi nhật ký Python

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?

Làm cách nào để lấy tệp XML từ URL?

Cách đọc dữ liệu XML từ URL..
Sao chép các cuốn sách. ....
Open Visual Studio ..
Tạo một ứng dụng giao diện điều khiển C# Visual mới. ....
Chỉ định sử dụng chỉ thị trên hệ thống. ....
Tạo một thể hiện của lớp XMLTexTreader và chỉ định URL. ....
Đọc qua XML. ....
Kiểm tra các nút. ....
Kiểm tra các thuộc tính ..

Làm thế nào phân tích url xml trong python?

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 (https://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.using the urlopen method of urllib. request library and then it will parse and read the XML data using parse method of library xml. etree.

Làm cách nào để xem các tệp 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 ().

Xmltodict Python là gì?

XMLTODICT: Đây là một mô -đun Python giúp làm việc với XML cảm thấy như bạn đang làm việc với [JSON].Chạy lệnh sau trong thiết bị đầu cuối để cài đặt mô -đun.a Python module that makes working with XML feel like you are working with [JSON]. Run the following command in the terminal to install the module.