我正在尝试抓取以下 html (1217428) 的 id,而不抓取 id 标签的其余部分,但我不知道如何仅隔离所需的部分。
<td class="pb-15 text-center">
<a href="#" id="1217428_1_10/6/2020 12:00:00 AM" class="slotBooking">
8:15 AM ✔
</a>
</td>
到目前为止,我想出了这个:
lesson_id = [] # I wish to fit the lesson id in this list
soup = bs(html, "html.parser")
slots = soup.find(attrs={"class" : "pb-15 text-center"})
tag = slots.find("a")
ID = tag.attrs["id"]
print (ID)
但这仅允许我将其作为输出接收:
1217428_1_10/6/2020 12:00:00 AM
有什么方法可以编辑我的代码,使输出为:
1217428
我也尝试过使用正则表达式:
lesson_id = []
soup = bs(html, "html.parser")
slots = soup.find(attrs={"class" : "pb-15 text-center"})
tag = slots.find("a")
ID = tag.attrs["id"]
lesson_id.append(ID(re.findall("\d{7}")))
但我收到此错误:
TypeError: findall() missing 1 required positional argument: 'string'