确定HTML项是否具有Python和BeautifulSoup的类

2024-06-01 02:43:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想确定li项目是否有该类。corsa是的。如果是,我想附加到一个数组'STATUS':“active”。数据是从this website中刮取的 我试过下面的代码,但是

if "corsa-yes" in next_li.get("class"):
TypeError: argument of type 'NoneType' is not iterable

这是我的密码

medmar_live_departures_table = list(soup.select('li.tratta'))
departure_time = []
    for li in medmar_live_departures_table:
        next_li = li.find_next_sibling("li")
        while next_li and next_li.get("data-toggle"):
            departure_time.append(next_li.strong.text)
            next_li = next_li.find_next_sibling("li")
        medmar_live_departures_data.append({
              'ROUTE' : li.text,
              'DEPARTURE TIME' : departure_time,
        })
             if "corsa-yes" in next_li.get("class"):
                medmar_live_departures_data.append({
                       'STATUS': "active" 
                })

Tags: inlivedatagetiftimestatusli
1条回答
网友
1楼 · 发布于 2024-06-01 02:43:35

错误消息

TypeError: argument of type 'NoneType' is not iterable

因为元素有no class,您需要首先检查它是否存在或与数组进行比较

if next_li.get("class") == ["corsa-yes"]:
# or check it first
if next_li.get("class") and "corsa-yes" in next_li.get("class"):

我将'STATUS': "active"更改为'ACTIVE_TIME': '10:35',并完成代码

departure_time = []
active_time = None
for li in medmar_live_departures_table:
    next_li = li.find_next_sibling("li")
    while next_li and next_li.get("data-toggle"):
        if next_li.get("class") == ["corsa-yes"]:
            active_time = next_li.strong.text
        departure_time.append(next_li.strong.text)
        next_li = next_li.find_next_sibling("li")
    medmar_live_departures_data.append({
          'ROUTE' : li.text,
          'ACTIVE_TIME' : active_time,
          'DEPARTURE TIME' : departure_time
    })
    departure_time = []

结果

[
  {'ROUTE': 'ISCHIA » PROCIDA', 'ACTIVE_TIME': '10:35', 'DEPARTURE TIME': ['06:25', '10:35']},
  {'ROUTE': 'PROCIDA » NAPOLI', 'ACTIVE_TIME': '07:05', 'DEPARTURE TIME': ['07:05', '11:15']}
]

相关问题 更多 >