首页 > 解决方案 > 跨度解析python

问题描述

我是这里的新用户。我在通过 beautifullsoup(python) 解析数据时遇到问题。我有这样的跨度:

<span class="kolesa-score-label cheaper">
    <span class="kolesa-score-label-on">на</span> 
    2.93%&nbsp;дешевле
</span>

我需要得到 2.93

deshevle = soup.find_all('span', class_='kolesa-score-label cheaper').text

不管用。

还有一个问题是:

for div in soup.find_all('div', attrs={'class': 'row vw-item list-item blue a-elem'}):
    adv_id=str(div.find('a')['data-product-id'])

它只记录最后一个周期。为什么?因为我想把它当作清单......已经尝试过adv_id=[],没有任何改变

标签: pythonhtmlparsing

解决方案


adv_id=str(div.find('a')['data-product-id'])

试试 adv_id.append 吧?(在 adv_id=[] ofc 之后)

但如果你想要一个列表,你应该像一个列表一样思考,根本不使用 append:

adv_id = [str(div.find('a')['data-product-id']) for div in soup.find_all('div', attrs={'class': 'row vw-item list-item blue一个元素'})]

列表理解: https ://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

PS:不知道你的美丽汤,我只是在这里嫖一些蟒蛇的名声


推荐阅读