python - 如何
在使用python提取文本和href时删除标签中存在的标签
问题描述
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)
推荐阅读
- javascript - 使用 Service Worker,我们如何链接更新数据调用之后的 fetch 调用的后续请求?
- c# - 我希望将列表中的请求状态更改为在 MVC 中接受或拒绝
- azure - 为什么在最近的 Azure API 管理升级后,以前稳定且有效的 Liquid 模板会失败?
- php - 使用 PHP 连接到远程 MSSQL 服务器不工作
- python - 在 create() 之前检查值是否有效
- php - 页面缓存时WordPress会触发index.php吗?
- java - IntelliJ Idea 不使用 Servlet,而是使用 JSP 页面
- laravel - 无法在主页部分显示类别明智的数据
- python - 如何从特定日期开始将日期转换为 int?Python
- python - Python 中的 K-Means 聚类存在问题