Python BeautifulSoup在类中找到span(Python BeautifulSoup find span inside class)

编程入门 行业动态 更新时间:2024-10-23 14:32:15
Python BeautifulSoup在类中找到span(Python BeautifulSoup find span inside class) python

我正在尝试创建一个python脚本,它在一个来自类的垃圾邮件中找到一个特定的测试。 不幸的是,我一直得到一个空的回应或“无”。

它来自一个非常具体的页面,所以生病粘贴它的一小部分,我试图找到:

<tbody> <tr class="zone-dedicated-availability" data-actions="refUnavailable" data-dc="" data-ref="160sk5" data-availability="3600-"> <td class="show-on-ref-unavailable elapsed-time-since-last-delivery" colspan="5"> <span qtlid="47402"> Last server delivered: today at 01:59. </span><br><a style="font- size:14px;" href=".." qtlid="50602">Go for a VPS-CLOUD<br><span style="font-size:0.9em;" qtlid="50615">(from £5.99 excl.VAT)</span></a> </td>

我试图从我的脚本中获取“最后一台服务器”tekst。 我还在学习,所以会很感激帮助:

page = requests.get('...') tree = page.content soup = BeautifulSoup(tree) table = soup.find('tbody', {'class': 'zone-dedicated-availability'}) print table

我可能在查找语句中遗漏了一些,因为这是我现在停留的地方,尝试了一些不同的东西,但不知道我怎么能得到像我需要的有效输出。

I am trying to create a python script which finds a specific test inside a spam which comes from a class. Unfortunately i keep getting an empty response or "none".

It comes from a very specific page so ill paste a small bit of it which im trying to find:

<tbody> <tr class="zone-dedicated-availability" data-actions="refUnavailable" data-dc="" data-ref="160sk5" data-availability="3600-"> <td class="show-on-ref-unavailable elapsed-time-since-last-delivery" colspan="5"> <span qtlid="47402"> Last server delivered: today at 01:59. </span><br><a style="font- size:14px;" href=".." qtlid="50602">Go for a VPS-CLOUD<br><span style="font-size:0.9em;" qtlid="50615">(from £5.99 excl.VAT)</span></a> </td>

I am trying to get the "last server delivered" tekst from my script. I am still learning so would appreciate the help:

page = requests.get('...') tree = page.content soup = BeautifulSoup(tree) table = soup.find('tbody', {'class': 'zone-dedicated-availability'}) print table

I am probably missing some at the find statement as this is where im stuck at now, tried a few different things but not sure how i can get a valid output like i need to.

最满意答案

class属性是tr所以你需要使用它:

table = soup.find('tbody').find('tr', {'class': 'zone-dedicated-availability'})

甚至更好:

table = soup.find('tr', {'class': 'zone-dedicated-availability'})

您还可以使用CSS选择器和select方法:

soup.select('tbody tr.zone-dedicated-availability')

要获得所需的数据,请使用qtlid="47402"在第一个“span”中:

In [19]: soup.find('tr', class_='zone-dedicated-availability').find('span', qtlid='47402').get_text(strip=True) Out[19]: 'Last server delivered: today at 01:59.'

The class attribute is in tr so you need to use this:

table = soup.find('tbody').find('tr', {'class': 'zone-dedicated-availability'})

or even better:

table = soup.find('tr', {'class': 'zone-dedicated-availability'})

You can also use a CSS selector and the select method:

soup.select('tbody tr.zone-dedicated-availability')

To get the data you want is in the first "span" with qtlid="47402" thus:

In [19]: soup.find('tr', class_='zone-dedicated-availability').find('span', qtlid='47402').get_text(strip=True) Out[19]: 'Last server delivered: today at 01:59.'

更多推荐

本文发布于:2023-07-25 19:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   BeautifulSoup   Python   class   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!