本文介绍了如何使用Beautiful Soup在一个查询中获取多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下html中找到class ="s"或class ="sb"的td

I want to find td with class="s" or class="sb" in the following html

<tr bgcolor="#e5e5f3"><td class="sb" width="200" align="left">test1</td><td class="sb" align="right">5,774.0</td><td class="sb" align="right">4,481.0</td><td class="sb" align="right">5,444.0</td><td class="sb" align="right">6,615.0</td><td class="sb" align="right">6,858.0</td></tr>
<tr bgcolor="#f0f0E7"><td class="s" width="200" align="left">test2</td><td class="s" align="right">5,774.0</td><td class="s" align="right">4,481.0</td><td class="s" align="right">5,444.0</td><td class="s" align="right">6,615.0</td><td class="s" align="right">6,858.0</td></tr>

我现在正在使用以下代码.但是只能得到等于"S"的类.是否可以在一个Beautiful Soup find_all查询中同时获得"s"和"sb"?

I'm using the following code right now. But can only get class equal "S". Is it possible to get both "s" and "sb" in one Beautiful Soup find_all query?

 soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml");
 for item in soup.find_all("td", { "class" : "s" }):

推荐答案

您可以使用精美汤对正则表达式的支持来完成此操作.

You can do this using beautiful soup's support for regular expressions.

import re
soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml");
for item in soup.find_all("td", { "class" : re.compile(r"^(s|sb)$") })

此正则表达式匹配:

  • ^-字符串的开头

(s|sb)-字符串's'或字符串'sb'

$-字符串的结尾

这篇关于如何使用Beautiful Soup在一个查询中获取多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:56