0

我正在连接一个 API 以下载一些 xml 格式的网站。我在 Jupyter 环境中使用 BeautifulSoup。我正在使用此代码来访问数据...

my_params = {some parameters}
response = requests.get(base_url, params = my_params)
response = response.content
soup = BeautifulSoup(response, 'xml')
soup

使用此代码,我可以很好地以 xml 格式提取网站。这是一个xml的例子......

<result>
    <title>Sepsis Alliance</title>
    <contentType>html</contentType>
    <sum>...  and Sepsis Alliance has introduced new and exciting ways for individuals, medical  ... industry partners and other organizations to help shine the spotlight on sepsis. ... Spike Out Sepsis. Columbus, OH. July 23, 2016. 3rd Annual Jeffrey Ray Davis Sepsis  ... Sepsis Alliance News. June 4, 2016. Sepsis Alliance Mourns Loss of Boxing Legend Muhammad ...</sum>
    <url>www.sepsisalliance.org</url>...

我在每个中提取 10 个结果requests.get。我需要将此 xml 请求保存到硬盘中的 .xml 文件中,以便在 RStudio 中进一步工作。我正在使用以下代码来保存文件...

soup = str(soup)    
file_out = open('text_mining.xml', 'a')
file_out.write(soup)
file_out.close()

我遇到的问题是导出的 .xml 文件中的标签是空的,就像这样......

<result>
<title></title>
<contenttype></contenttype>
<sum></sum>
<url></url>
<hopcount>0</hopcount>
<size></size>

有哪些解决方案?

4

1 回答 1

0

我发现了我的错误。我不得不在这行代码中更改a标志...w

file_out = open('text_mining.xml', 'a') # before
file_out = open('text_mining.xml', 'w') # after
于 2016-09-29T03:16:35.133 回答