Python:找到所有tr孩子的第二个孩子

时间:2017-10-16 17:14:08

标签: python selenium xpath css-selectors

我的目标是获取所有法庭案件编号并将其放入Excel文件夹中。案件在第二栏

我的代码:

courtCases = driver.find_elements_by_css_selector('body > table:nth-child(3) > tbody > tr:nth-child* > td:nth-child(2)')
for courtCase in courtCases:
    print(courtCase.text)

这会抛出错误

  

selenium.common.exceptions.InvalidSelectorException:消息:无效的选择器:指定了无效或非法的选择器。

通过将确切的css路径和xpath放在一起,我得到了一个法庭案例:

courtCases = driver.find_elements_by_css_selector('body > table:nth-child(3) > tbody > tr:nth-child(7) > td:nth-child(2) > font')

我需要收集第二栏td:nth-​​child(2)中的所有法庭。

无论如何我的问题是:任何人都可以帮我写一个好的css选择器或xpath来获得所有的法庭日期吗?

一些html

<html>
<head>
<title>Wejis - Dayton Municipal Court</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="750" border="0">
  <tr>
    <td width="185"><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">Run 
      Date: 10/16/2017 </font></td>
    <td width="380"><div align="center">
        <p><strong><font color="#003399" size="4" face="Verdana, Arial, Helvetica, sans-serif">Housing 
          Docket Report</font></strong></p>
        <p><strong><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">Dayton 
          Municipal Court</font></strong></p>
      </div></td>
    <td width="185"><div align="right"><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">Run 
       Time: 12:28 PM</font></div></td>
  </tr>
</table>
<table width="750">
    <tr><td colspan="4">&nbsp;</td></tr>
    <tr>
        <td width="250"><strong><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">Court Date: September 20, 2017</font></strong></td>
        <td width="140"><strong><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">All Sessions</font></strong></td>
        <td width="130"><div align="center"><strong><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">Courtroom 3A</font></strong></div></td>
        <td width="220"><div align="right"><strong><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif">Judge Deirdre E Logan</font></strong></div></td>
    </tr>
</table>
<table width="750" border="0">
  <tr> 
    <td colspan="5"><hr></td>
  </tr>

            <tr> 
                <td colspan="2"><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Housing Trial</strong></font></td>
                <td colspan="3"><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong> 8:30AM </strong></font></td>
            </tr>
            <tr> 
                <td width="140"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Defendant Name</font></strong></td>
                <td width="120"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Case Number</font></strong></td>
                <td width="240"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Charges</font></strong></td>
                <td width="115"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Attorney</font></strong></td>
                <td width="115"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Location</font></strong></td>
            </tr>

                <tr> 
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Rosal, Jorge</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">2017-CRM-005695</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">MAINTAINING EXTERIOR<br></font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif"></font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">1347  Kingsley </font></td>
                </tr>

            <tr> 
                <td colspan="2"><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Criminal Court Trial In Jail</strong></font></td>
                <td colspan="3"><font color="#003399" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong> 9:30AM </strong></font></td>
            </tr>
            <tr> 
                <td width="140"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Defendant Name</font></strong></td>
                <td width="120"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Case Number</font></strong></td>
                <td width="240"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Charges</font></strong></td>
                <td width="115"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Attorney</font></strong></td>
                <td width="115"><strong><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Location</font></strong></td>
            </tr>

                <tr> 
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Joyner, Melissa</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">2017-CRB-000784</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">DRUG ABUSE INSTRUMENT<br>DRUG PARAPHERNALIA/USE OR POSS<br></font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Jenn A. Cunningham-Minnick</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">1401  Harshman RD</font></td>
                </tr>

                <tr> 
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">Joyner, Melissa</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">2017-CRM-000775</font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">LITTERING IN PARK<br></font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif"></font></td>
                    <td valign="top"><font color="#003399" size="1" face="Verdana, Arial, Helvetica, sans-serif">1401  Harshman RD</font></td>
                </tr>

2 个答案:

答案 0 :(得分:1)

通过XPath找到它:

courtCases = driver.find_elements_by_xpath('//td[2]/font[@size="1"]')
for courtCase in courtCases:
    print(courtCase.text)

请注意所有法庭案件的字体大小 1 。如果你遗漏了这个属性,你也会得到时间。

答案 1 :(得分:1)

你的选择器比你需要的多一点。我发现它可以减少到下面。

td:nth-child(2) > font[size='1']

CSS选择器比XPath更快更好地受支持,但是有一些东西,比如通过包含的文本定位元素,只有XPath可以做。