Python BeautifulSoup 给 findAll 多个标签

标签 python beautifulsoup

我正在寻找一种方法来使用 findAll 来获取两个标签,按照它们在页面上出现的顺序。

目前我有:

import requests
import BeautifulSoup

def get_soup(url):
    request = requests.get(url)
    page = request.text
    soup = BeautifulSoup(page)
    get_tags = soup.findAll('hr' and 'strong')
    for each in get_tags:
        print each

如果我在只有“em”或“strong”的页面上使用它,那么它会得到所有这些标签,如果我在一个页面上同时使用它会得到“strong”标签。

有没有办法做到这一点?我主要关心的是保留找到标签的顺序。

最佳答案

你可以 pass a list , 查找任何给定的标签:

tags = soup.find_all(['hr', 'strong'])

关于Python BeautifulSoup 给 findAll 多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20648660/

相关文章:

python - BeautifulSoup </div> 抓取突然停止工作

python - Beautifulsoup : Difference between . find() 和 .select()

python - 在 python 中进行 Squish 测试以测试 qt 应用程序

python - 如何使用 Scrapy DownloadMiddleware 重试 503 响应?

python - 简化 Pandas 中的 bool 索引条件

python - 从 for 循环写入文件

python - 将utf-8格式的字符串转换为unicode : Python

python - 在 python 中使用 boto3 查询 cloudwatch 日志中的不同值

python - 带有鼠标悬停的html网页将数据转换为json

python - Beautiful Soup - 下载 css 元素