BeautifulSoup使用两个不同的div类查找数据

时间:2018-06-23 22:29:17

标签: python html beautifulsoup

我正在尝试解析此html网页,并且我想要网页中的特定数据。我在这个特定的类上使用findall函数。唯一的问题是,由于类冲突,它正在网站的一侧获取不必要的数据。我基本上想对包含“ mainContent”和项目类名称的div类执行findall。我只找到了如何查找包含“空白”或“空白”的类。试图找到类名称为“ blank”和“ blank”的东西的语法是什么?谢谢!

1 个答案:

答案 0 :(得分:0)

使用css selectors代替find_all():

#! /usr/bin/env python3
# -*- coding: UTF8 -*-from bs4 import BeautifulSoup

html_doc = """
<!DOCTYPE html>
<html lang="en-US">
<head>
  <title>test</title>
</head>
<body>
  <div class="alpha">not a match.</div>
  <div class="beta">not a match.</div>
  <div class="alpha beta">match.</div>
  <div class="beta alpha">match.</div>
  <p class="alpha beta">not match.</div>
</body>
</html>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.select("div.alpha.beta"))