How to get multiple class in one query using Beautiful Soup

折月煮酒 提交于 2020-01-12 10:18:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!