python - Python BeautifulSoup 提取
问题描述
我已使用以下代码访问下面发布的描述。
这是代码:
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>
<ul>
<li><img src="http://meteoclimatic.net/img/sem_tpv.png" style="width: 12px; height: 12px; border: 0px;" alt="***" /> <a href="http://www.meteoclimatic.net/perfil/ESCYL2400000024153A">Sta Mar&#237;a del Condado</a></li>
<ul>
<li> Actualizado: 24-07-2018 08:20 UTC</li>
<li>Temperatura: <b>23,6</b> &#186;C (
M&#225;x.: <b style="color: red">23,6</b> /
M&#237;n.: <b style="color: blue">12,1</b> )</li>
<li>Humedad: <b>54,0</b> % (
M&#225;x.: <b style="color: red">91,0</b> /
M&#237;n.: <b style="color: blue">54,0</b> )</li>
<li>Bar&#243;metro: <b>1021,0</b> hPa (
M&#225;x.: <b style="color: red">1021,2</b> /
M&#237;n.: <b style="color: blue">1019,9</b> )</li>
<li>Viento: <b>1,0</b> km/h (
M&#225;x.: <b style="color: red">9,0</b> )</li>
<li>Direcci&#243;n del viento: <b>170</b> - S</li>
<li>Precip.: <b>0,0</b> mm</li>
</ul>
</ul>
<!--
[[<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ía del Condado>]]
[[<END:ESCYL2400000024153A:DATA>]]
-->
</description>
我想提取标签[[<BEGIN:ESCYL2400000024153A:DATA>]]
和[[<END:ESCYL2400000024153A:DATA>]]
. 我怎么能以“pythonic”的方式做到这一点,而不必手动将每个项目解析为字符串?
编辑:
我要提取的数据也可能在这部分汤里找到:
<ul>
<li><img src="http://meteoclimatic.net/img/sem_tpv.png" style="width: 12px; height: 12px; border: 0px;" alt="***" /> <a href="http://www.meteoclimatic.net/perfil/ESCYL2400000024153A">Sta Mar&#237;a del Condado</a></li>
<ul>
<li> Actualizado: 24-07-2018 08:50 UTC</li>
<li>Temperatura: <b>24,4</b> &#186;C (
M&#225;x.: <b style="color: red">24,5</b> /
M&#237;n.: <b style="color: blue">12,1</b> )</li>
<li>Humedad: <b>49,0</b> % (
M&#225;x.: <b style="color: red">91,0</b> /
M&#237;n.: <b style="color: blue">49,0</b> )</li>
<li>Bar&#243;metro: <b>1021,0</b> hPa (
M&#225;x.: <b style="color: red">1021,2</b> /
M&#237;n.: <b style="color: blue">1019,9</b> )</li>
<li>Viento: <b>5,0</b> km/h (
M&#225;x.: <b style="color: red">10,0</b> )</li>
<li>Direcci&#243;n del viento: <b>219</b> - SW</li>
<li>Precip.: <b>0,0</b> mm</li>
</ul>
</ul>
解决方案
你可以用 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ía del Condado>]]']
编辑:
此代码遍历所有<item>
标签。在每个<item>
标签中,它会在<description>
所有文本中找到Comment
对象的实例(换句话说,任何介于<!--
和-->
标签之间的东西。然后它将根据换行符拆分第一条评论并写入除第一行和最后一行之外的所有行。
推荐阅读
- android - Android - 更改 NavigationView 中单个项目的背景颜色
- javascript - 更改缩放级别 3d scatterPlot (Plotly)
- html - 防止包含跨度的直通
- java - 如何在 Java 中为临时目录配置 root
- data-structures - 在第二个哈希函数中使用复合数进行双重哈希
- python - Robot Framework IDE Python SendKeys 库
- powershell - Powershell将文本文件中的项目添加到二维数组
- scala - 在Scala中创建元素差异为1的序列组?
- oracle - 是否可以将 Oracle PL/SQL 触发器作为函数和包?
- android - 更改标签宽度