Python BeautifulSoup 为 findAll 提供了多个标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20648660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python BeautifulSoup give multiple tags to findAll
提问by DasSnipez
I'm looking for a way to use findAll to get two tags, in the order they appear on the page.
我正在寻找一种使用 findAll 获取两个标签的方法,按照它们出现在页面上的顺序。
Currently I have:
目前我有:
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.
如果我在一个只有 'em' 或 'strong' 的页面上使用它,那么它会给我所有这些标签,如果我在一个同时使用它会得到 'strong' 标签。
Is there a way to do this? My main concern is preserving the order in which the tags are found.
有没有办法做到这一点?我主要关心的是保留找到标签的顺序。
采纳答案by jfs
You could pass a list, to find any of the given tags:
您可以传递一个 list,以查找任何给定的标签:
tags = soup.find_all(['hr', 'strong'])
回答by TerryA
Use regular expressions:
使用正则表达式:
import re
get_tags = soup.findAll(re.compile(r'(hr|strong)'))
The expression r'(hr|strong)'will find either hrtags or strongtags.
该表达式r'(hr|strong)'将查找hr标签或strong标签。

