解析 python 中的 XML 对象 3.9
Parsing XML object in python 3.9
我正在尝试使用 NCBI API 获取一些数据。我正在使用 requests
连接到 API。
我遇到的问题是如何将请求 returns 的 XML 对象转换为我可以解析的对象?
到目前为止,这是我的函数代码:
def getNCBIid(speciesName):
import requests
base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
url = base_url + "esearch.fcgi?db=assembly&term=(%s[All Fields])&usehistory=y&api_key=f1e800ad255b055a691c7cf57a576fe4da08" % speciesName
#xml object
api_request = requests.get(url)
你可以为此使用 BeautifulSoup 之类的东西('this' 是 'convert and parse the xml object')。
您调用的xml对象仍然是响应对象,您需要先从该对象中提取内容。
from bs4 import BeautifulSoup
def getNCBIid(speciesName):
import requests
base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
url = base_url + "esearch.fcgi?db=assembly&term=(%s[All Fields])&usehistory=y&api_key=f1e800ad255b055a691c7cf57a576fe4da08" % speciesName
#xml object. <--- this is still just your response object
api_request = requests.get(url)
# grab the response content
xml_content = api_request.content
# parse with beautiful soup
soup = BeautifulSoup(xml_content, 'xml')
# from here you would access desired elements
# here are docs: https://www.crummy.com/software/BeautifulSoup/bs4/doc/
我正在尝试使用 NCBI API 获取一些数据。我正在使用 requests
连接到 API。
我遇到的问题是如何将请求 returns 的 XML 对象转换为我可以解析的对象?
到目前为止,这是我的函数代码:
def getNCBIid(speciesName):
import requests
base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
url = base_url + "esearch.fcgi?db=assembly&term=(%s[All Fields])&usehistory=y&api_key=f1e800ad255b055a691c7cf57a576fe4da08" % speciesName
#xml object
api_request = requests.get(url)
你可以为此使用 BeautifulSoup 之类的东西('this' 是 'convert and parse the xml object')。
您调用的xml对象仍然是响应对象,您需要先从该对象中提取内容。
from bs4 import BeautifulSoup
def getNCBIid(speciesName):
import requests
base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
url = base_url + "esearch.fcgi?db=assembly&term=(%s[All Fields])&usehistory=y&api_key=f1e800ad255b055a691c7cf57a576fe4da08" % speciesName
#xml object. <--- this is still just your response object
api_request = requests.get(url)
# grab the response content
xml_content = api_request.content
# parse with beautiful soup
soup = BeautifulSoup(xml_content, 'xml')
# from here you would access desired elements
# here are docs: https://www.crummy.com/software/BeautifulSoup/bs4/doc/