Python的BeautifulSoup给多个标签的findAll [英] Python BeautifulSoup give multiple tags to findAll

查看:2062
本文介绍了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

如果我用一个页面上,只有'时间'或'强'在它,然后它会得到我所有的标签,如果我在一个既会得到强的标签使用。

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.

有没有办法做到这一点?我主要关注的是preserving在该标签被发现的顺序。

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 either hr or strong tags:

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

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

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