当班级具有随机offerid时,如何使用beautifulsoup查找所有班级?我正在尝试使用以下语法,但没有任何帮助
containers = page_soup.findAll("div",{"class":xyz "})
类示例如下
<div class=abc">
<div class=bcd">
<div class="xyz " offerid="65546">
<div class="xyz " offerid="46465">
<div class="xyz " offerid="56747">
</div>
</div>
答案 0 :(得分:1)
使用CSS选择器获取具有类名称的项目。请尝试以下示例。
data = '''
<div class="xyz " offerid="65546">Test 1</div>
<div class="xyz " offerid="46465">Test 2</div>
<div class="xyz " offerid="56747">Test 3</div>
'''
soup = BeautifulSoup(data,'html.parser')
containers = soup.select("div.xyz")
for item in containers:
print(item.text)
OR
data = '''
<div class="xyz " offerid="65546">Test 1</div>
<div class="xyz " offerid="46465">Test 2</div>
<div class="xyz " offerid="56747">Test 3</div>
'''
soup = BeautifulSoup(data,'html.parser')
containers = soup.select(".xyz")
for item in containers:
print(item.text)
已编辑
:data = '''
<div class=abc">
<div class=bcd">
<div class="xyz " offerid="65546">
<div class="xyz " offerid="46465">
<div class="xyz " offerid="56747">
</div>
</div>
'''
soup = BeautifulSoup(data,'html.parser')
containers = soup.select("div.xyz[offerid]")
for item in containers:
print(item['offerid'])
答案 1 :(得分:0)
我不清楚您的问题是否是100%清楚的,但是如果您通过类名来选择div,则ID无关紧要。下面的示例代码有效。
html = '''
<div class="xyz " offerid="65546"> sample text </div>
<div class="xyz " offerid="46465"> sample text </div>
<div class="xyz " offerid="56747"> sample text </div>
'''
soup = BeautifulSoup(html,'html.parser')
containers = soup.find_all("div",{"class":"xyz "})
答案 2 :(得分:0)
也许您想要一个属性选择器
parental_line
或
data = [item['offerid'] for item in soup.select('[offerid]')]