我正在使用BeautifulSoup从html提取标签。有一些具有多个CSS类示例的html标记:
DispatchQueue.main.async {
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 6.0, repeats: false)
let request = UNNotificationRequest(identifier: "myNotification", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil{
print(error!)
completionHandler(.failed) // Add this line
} else {
completionHandler(.noData) // Add this line
}
}
}
当我使用上面的代码时,它同时给了我两个标签。有什么办法可以让我获得包含所有css的元素。
答案 0 :(得分:0)
这将找到所有具有'class'属性并具有所有指定类的标签(a,div或其他标签)
from bs4 import BeautifulSoup
html = '''
<a class='a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal'>This is a anchor text</a>
<div class='s-access-detail-page s-color-twister-title-link a-text-normal'>Div text</div>
'''
soup = BeautifulSoup(html, "lxml")
all_prod_links = soup.find_all(
lambda t: 'class' in t.attrs and 'a-link-normal' in t['class'] and \
's-access-detail-page' in t['class'] and \
's-color-twister-title-link' in t['class'] and \
'a-text-normal' in t['class'])
print(all_prod_links)
打印:
[<a class="a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal">This is a anchor text</a>]
答案 1 :(得分:0)
html = '''
<a class ='a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal'>This is a anchor text</a>
<div class ='s-access-detail-page s-color-twister-title-link a-text-normal'>Div text</div>
'''
soup = BeautifulSoup(html, "lxml")
all_prod_links = soup.find_all(attrs={'class':'a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal'})
结果是
[<a class="a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal">This is a anchor text</a>]