代码之家  ›  专栏  ›  技术社区  ›  Em Ae

选择具有类名的特定行

  •  1
  • Em Ae  · 技术社区  · 7 年前

    我正在分析一个HTML,它有许多行要选择。下面是这些行的示例

    <tr class="constantstring-randomvalue1-row" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue1-row'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="constantstring-randomvalue1-row" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue1-row'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="constantstring-randomvalue2-row-2" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue2-row-2'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="constantstring-randomvalue2-row-2" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue2-row-2'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    

    我想做的是利用 BeautifulSoup4 find_all 使用正则表达式 find_all(re.compile(regext))

    然而,问题是我无法想出一个好的regext,它将选择我感兴趣的所有行。

    我要从所有的行开始 constantstring- . 我不管后面是什么。我应该用什么方法 re.compile 如果是这样,正确的方法是什么? regex ?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Ian-Fogelman    7 年前

    如果您想通过re来完成这一点,下面将做一些工作,我添加了一个额外的行来演示它不拾取最后一行。

    http://rextester.com/OSSFB8621

    from bs4 import BeautifulSoup
    import re
    html ="""
    <tr class="constantstring-randomvalue1-row" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue1-row'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="constantstring-randomvalue1-row" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue1-row'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="constantstring-randomvalue2-row-2" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue2-row-2'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="constantstring-randomvalue2-row-2" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue2-row-2'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    <tr class="axcconstantstring-randomvalue2-row-2" onmouseover="this.className='constantstring-light-row-cp-h'" onmouseout="this.className='constantstring-randomvalue2-row-2'" onclick="if(ignoreOnClick==false)window.location='find.ashx?cv3dsw'" valign="top">
    """
    bs = BeautifulSoup(html,'lxml')
    for tr in bs.find_all("tr", {"class" : re.compile('^(constantstring)')}):
        print(tr)
    
        2
  •  0
  •   SanthoshSolomon    7 年前

    您可以为同一任务使用内置的字符串方法,而不是regex。像,

    rows = soup.find_all('tr)'
    selected_rows = [i for i in rows if str(i).startswith('tr class="constantstring-randomvalue')]
    

    如果你错过了 str() 这个 if 条件将失败。

    希望这有帮助!干杯!