gpt4 book ai didi

python - 如何使用 python 从网站获取 XML 文件?

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:38 27 4
gpt4 key购买 nike

使用“bottle”库,我必须基于此网站创建自己的API http://dblp.uni-trier.de所以我必须获取每个作者的数据。因此,我使用以下链接格式 http://dblp.uni-trier.de/pers/xx/ '姓氏的第一个字母'/'lastnamefirstname'.xml

您能帮我获取 XML 格式吗?以便能够解析它并获取我需要的信息。谢谢

import bottle
import requests
import re

r = requests.get("https://dblp.uni-trier.de/")

#the format of my request is
#http://localhost:8080/lastname firstname

@bottle.route('/info/<name>')
def info(name):

first_letter = name[:1]

#mettre au format Lastname:Firstname
...

data = requests.get("http://dblp.uni-trier.de/pers/xx/" + first_letter + "/" + family_name + ".xml")

return data

bottle.run(host='localhost', port=8080)

最佳答案

from xml.etree import ElementTree
import requests

url = 'some url'
response = requests.get(url)
xml_root = ElementTree.fromstring(response.content)

fromstring Parses an XML section from a string constant. This function can be used to embed “XML literals” in Python code. text is a string containing XML data. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns an Element instance.

HOW TO Load XML from a string into an ElementTree

from xml.etree import ElementTree
root = ElementTree.fromstring("<root><a>1</a></root>")
ElementTree.dump(root)
OUTPUT
<root><a>1</a></root>

关于python - 如何使用 python 从网站获取 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55846737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com