【问题标题】:Python BeautifulSoup extractionPython BeautifulSoup 提取
【发布时间】:2018-07-24 17:22:31
【问题描述】:

我已使用以下代码访问下面发布的描述。

代码如下:

import requests
from bs4 import BeautifulSoup

resp = requests.get('https://www.meteoclimatic.net/feed/rss/ESCYL2400000024153A')
soup = BeautifulSoup(resp.content, features='xml')
items = soup.findAll('item')
print(items[0].description)

我获得了以下 XML 示例:

<description>

     &lt;ul&gt;
&lt;li&gt;&lt;img src="http://meteoclimatic.net/img/sem_tpv.png" style="width: 12px; height: 12px; border: 0px;" alt="***" /&gt; &lt;a href="http://www.meteoclimatic.net/perfil/ESCYL2400000024153A"&gt;Sta Mar&amp;#237;a del Condado&lt;/a&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt; Actualizado: 24-07-2018 08:20 UTC&lt;/li&gt;
&lt;li&gt;Temperatura: &lt;b&gt;23,6&lt;/b&gt; &amp;#186;C (
M&amp;#225;x.: &lt;b style="color: red"&gt;23,6&lt;/b&gt; /
M&amp;#237;n.: &lt;b style="color: blue"&gt;12,1&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Humedad: &lt;b&gt;54,0&lt;/b&gt; % (
M&amp;#225;x.: &lt;b style="color: red"&gt;91,0&lt;/b&gt; /
M&amp;#237;n.: &lt;b style="color: blue"&gt;54,0&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Bar&amp;#243;metro: &lt;b&gt;1021,0&lt;/b&gt; hPa (
M&amp;#225;x.: &lt;b style="color: red"&gt;1021,2&lt;/b&gt; /
M&amp;#237;n.: &lt;b style="color: blue"&gt;1019,9&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Viento: &lt;b&gt;1,0&lt;/b&gt; km/h (
M&amp;#225;x.: &lt;b style="color: red"&gt;9,0&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Direcci&amp;#243;n del viento: &lt;b&gt;170&lt;/b&gt; - S&lt;/li&gt;
&lt;li&gt;Precip.: &lt;b&gt;0,0&lt;/b&gt; mm&lt;/li&gt;
&lt;/ul&gt;
     &lt;/ul&gt;

<!--
[[<BEGIN:ESCYL2400000024153A:DATA>]]
[[<ESCYL2400000024153A;(23,6;23,6;12,1;sun);(54,0;91,0;54,0);(1021,0;1021,2;1019,9);(1,0;9,0;170);(0,0);Sta Mar&#237;a del Condado>]]
[[<END:ESCYL2400000024153A:DATA>]]
-->
</description>

我想提取标签[[&lt;BEGIN:ESCYL2400000024153A:DATA&gt;]][[&lt;END:ESCYL2400000024153A:DATA&gt;]] 之间包含的项目。我怎么能以“pythonic”的方式做到这一点,而不必手动将每个项目解析为字符串?

编辑:

我要提取的数据也可能在这部分汤里找到:

&lt;ul&gt;
&lt;li&gt;&lt;img src="http://meteoclimatic.net/img/sem_tpv.png" style="width: 12px; height: 12px; border: 0px;" alt="***" /&gt; &lt;a href="http://www.meteoclimatic.net/perfil/ESCYL2400000024153A"&gt;Sta Mar&amp;#237;a del Condado&lt;/a&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt; Actualizado: 24-07-2018 08:50 UTC&lt;/li&gt;
&lt;li&gt;Temperatura: &lt;b&gt;24,4&lt;/b&gt; &amp;#186;C (
M&amp;#225;x.: &lt;b style="color: red"&gt;24,5&lt;/b&gt; /
M&amp;#237;n.: &lt;b style="color: blue"&gt;12,1&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Humedad: &lt;b&gt;49,0&lt;/b&gt; % (
M&amp;#225;x.: &lt;b style="color: red"&gt;91,0&lt;/b&gt; /
M&amp;#237;n.: &lt;b style="color: blue"&gt;49,0&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Bar&amp;#243;metro: &lt;b&gt;1021,0&lt;/b&gt; hPa (
M&amp;#225;x.: &lt;b style="color: red"&gt;1021,2&lt;/b&gt; /
M&amp;#237;n.: &lt;b style="color: blue"&gt;1019,9&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Viento: &lt;b&gt;5,0&lt;/b&gt; km/h (
M&amp;#225;x.: &lt;b style="color: red"&gt;10,0&lt;/b&gt; )&lt;/li&gt;
&lt;li&gt;Direcci&amp;#243;n del viento: &lt;b&gt;219&lt;/b&gt; - SW&lt;/li&gt;
&lt;li&gt;Precip.: &lt;b&gt;0,0&lt;/b&gt; mm&lt;/li&gt;
&lt;/ul&gt;
     &lt;/ul&gt;

【问题讨论】:

  • 这些在 XML 注释 &lt;!-- --&gt; 中,它们不是描述的一部分。

标签: python beautifulsoup


【解决方案1】:

您可以使用 BeautifulSoup 来做到这一点,使用 Comment 对象:

import requests
from bs4 import BeautifulSoup, Comment

resp = requests.get('https://www.meteoclimatic.net/feed/rss/ESCYL2400000024153A')
soup = BeautifulSoup(resp.content, 'xml')
for item in soup.select('item'):
    comments = item.description.find_all(text=lambda text:isinstance(text, Comment))
    print([c for c in comments[0].split('\n') if c][1:-1])

打印:

['[[<ESCYL2400000024153A;(24,4;24,5;12,1;sun);(49,0;91,0;49,0);(1021,0;1021,2;1019,9);(5,0;10,0;219);(0,0);Sta Mar&#237;a del Condado>]]']

编辑:

此代码遍历所有 &lt;item&gt; 标记。在每个&lt;item&gt; 标签中,它将在&lt;description&gt; 中找到所有文本,即Comment 对象的实例(换句话说,任何介于&lt;!----&gt; 标签之间的内容。然后它将根据换行符拆分第一条评论并写入除了第一行和最后一行之外的所有行。

【讨论】:

  • 您好,请您稍微解释一下您在这里做什么?非常感谢。
【解决方案2】:

使用lxml 获取description 元素中的XML 注释。

from lxml import etree

tree = etree.parse("so.xml")

comment = tree.xpath("/rss/channel/item/description/comment()")[0].text
print(comment.split("\n")[2])

输出:

[[<ESCYL2400000024153A;(24,4;24,5;12,1;sun);(49,0;91,0;49,0);(1021,0;1021,2;1019,9);(5,0;10,0;219);(0,0);Sta Mar&#237;a del Condado>]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2013-01-29
    • 2023-03-25
    • 2013-02-25
    • 2018-06-14
    相关资源
    最近更新 更多