Python BeautifulSoup 为 findAll 提供了多个标签 [英] Python BeautifulSoup give multiple tags to findAll

查看:19
本文介绍了Python BeautifulSoup 为 findAll 提供了多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to use findAll to get two tags, in the order they appear on the page.

目前我有:

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' 标签.

If I use that on a page with only 'em' or 'strong' in it then it will get me all of those tags, if I use on one with both it will get 'strong' tags.

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

Is there a way to do this? My main concern is preserving the order in which the tags are found.

推荐答案

你可以 传递一个列表,以查找任何给定的标签:

You could pass a list, to find any of the given tags:

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

这篇关于Python BeautifulSoup 为 findAll 提供了多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆