问题
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>
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" }):
回答1:
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)$") })
This regular expression matches:
^
- the start of the string(s|sb)
- either the string's'
or the string'sb'
$
- the end of the string
来源:https://stackoverflow.com/questions/13572676/how-to-get-multiple-class-in-one-query-using-beautiful-soup