美汤根据部分属性值查找标签

Win*_*981 4 python beautifulsoup

我正在尝试根据属性值的一部分来识别 html 文档中的标签。

例如,如果我有一个 Beautifulsoup 对象:

import bs4 as BeautifulSoup

r = requests.get("http:/My_Page")

soup = BeautifulSoup(r.text, "html.parser")
Run Code Online (Sandbox Code Playgroud)

我想要tr具有id其值格式如下的属性的标签:“news_4343_23255_xxx”。我对任何tr标签感兴趣,只要它有“新闻”作为id属性值的前 4 个字符。

我知道我可以搜索如下:

trs = soup.find_all("tr",attrs={"id":True})
Run Code Online (Sandbox Code Playgroud)

这给了我所有tr带有id属性的标签。

如何根据子字符串进行搜索?

Rak*_*esh 8

使用正则表达式来获得trid使用开始"news"

前任:

from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(html,  "html.parser")
for i in soup.find_all("tr", {'id': re.compile(r'^news')}):
    print(i)
Run Code Online (Sandbox Code Playgroud)