如何在使用 python 提取文本和 href 时删除标签中存在的 <br> 标签

How to remove<br> tag which is present in a tag while extracting text and href using python

text 和 href 的提取对所有国家/地区都非常有效,但对南非无效。

cookie下面url有国家列表,这里我只需要提取南非

差异[
]标签呈现如何在提取时删除

cookie_url = "https://www.unilevernotices.com/cookie-notice/notice.html"
response = requests.get(cookie_url)
soup = BeautifulSoup(response.content, 'html.parser')

market = soup.findAll('div', class_=re.compile('richText-content'))

market_linkd = soup.findAll('a', text=re.compile(("Spain - Spanish"),re.IGNORECASE))
print(" extracted remaining country data ", market_linkd)   # result works fine

market_linkd = soup.findAll('a', text=re.compile(("South Africa - English"),re.IGNORECASE)) #.replace('<br>','')
print(" South aftrica data ", market_linkd)  # result []

for ml in market_linkd:
    print("*********************", ml)
    response = requests.get('https://www.unilevernotices.com'+ml['href'])
    soup = BeautifulSoup(response.content, "html.parser")
    cookie_title = soup.find('h1', class_=re.compile('title-heading'))
    cookie_link = 'https://www.unilevernotices.com'+ml['href']
    print(cookie_link)
    print(cookie_title)  






output:
********************* <a href="/spain/spanish/cookie-notice/notice.html" title="Spain - Spanish  ">Spain - Spanish</a>
https://www.unilevernotices.com/spain/spanish/cookie-notice/notice.html
<h1 class="title-heading">Aviso de cookies</h1>

output:
 South Africa data  []

标题部分有白色 space 试试这个:

market_linkd = soup.findAll('a', title=re.compile("South Africa - English  "), href=True) #.replace('<br>','')
print(" South aftrica data ", market_linkd)