[英]How to remove<br> tag which is present in a tag while extracting text and href using python
text 和 href 的提取對所有國家/地區都非常有效,但不適用於南非。
下面的 cookie url 有國家列表,這里我只需要提取南非
差異 [<br>] 標簽展示了如何在提取時移除
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 []
標題部分有空格試試這個:
market_linkd = soup.findAll('a', title=re.compile("South Africa - English "), href=True) #.replace('<br>','')
print(" South aftrica data ", market_linkd)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.