Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Xpath从具有id的表中获取具有特定th值的tr_Python 3.x_Xpath_Lxml - Fatal编程技术网

Python 3.x 使用Xpath从具有id的表中获取具有特定th值的tr

Python 3.x 使用Xpath从具有id的表中获取具有特定th值的tr,python-3.x,xpath,lxml,Python 3.x,Xpath,Lxml,我将python3.6与XPath库一起使用。爬进桌子里给了我一张空名单。需要爬到特定的位置 我的tr内容是动态生成的。我需要爬网到具有特定th值的tr。例如,在HTML代码中,排名出现在第二个tr中,但它可以出现在tr中的任何位置。它没有特定的索引。需要从排名为th的tr获取href 我的html文件: <tbody> <tr> <th class="a-color-secondary a-size-base prodDetSe

我将python3.6与XPath库一起使用。爬进桌子里给了我一张空名单。需要爬到特定的位置

我的tr内容是动态生成的。我需要爬网到具有特定th值的tr。例如,在HTML代码中,排名出现在第二个tr中,但它可以出现在tr中的任何位置。它没有特定的索引。需要从排名为th的tr获取href

我的html文件:

   <tbody>
      <tr>
         <th class="a-color-secondary a-size-base prodDetSectionEntry">
            Product Number
         </th>
         <td class="a-size-base">
            B003NR57BY
         </td>
      </tr>

      <tr>
         <th class="a-color-secondary a-size-base prodDetSectionEntry">
             Rank
         </th>
         <td>
            <span>
            <span>#3 in <a href="/gp/bestsellers/pc/11036491/ref=pd_zg_hrsr_pc_1_1_last">Computer Mice</a></span>
            <br>
            </span>
         </td>
      </tr>

      <tr>
         <th class="a-color-secondary a-size-base prodDetSectionEntry">
            Created Date
         </th>
         <td class="a-size-base">
            June 7, 2010
         </td>
      </tr>
   </tbody>
</table>
我希望输出是

Rank: 3,
url : /gp/bestsellers/pc/11036491/ref=pd_zg_hrsr_pc_1_1_last,
category: Computer Mice
需要从排名为th的tr获取href

使用:

注意:这适用于您提供的片段(现在格式良好)。稍后需要添加上下文以选择
元素

<table> 
  <tbody> 
    <tr> 
      <th class="a-color-secondary a-size-base prodDetSectionEntry">Product Number</th>  
      <td class="a-size-base">B003NR57BY</td> 
    </tr>  
    <tr> 
      <th class="a-color-secondary a-size-base prodDetSectionEntry">Rank</th>  
      <td> 
        <span> 
          <span>#3 in 
            <a href="/gp/bestsellers/pc/11036491/ref=pd_zg_hrsr_pc_1_1_last">Computer Mice</a>
          </span>  
          <br/> 
        </span> 
      </td> 
    </tr>  
    <tr> 
      <th class="a-color-secondary a-size-base prodDetSectionEntry">Created Date</th>  
      <td class="a-size-base">June 7, 2010</td> 
    </tr> 
  </tbody> 
</table>

产品编号
B003NR57BY
等级
#3英寸

创建日期 2010年6月7日

在Python代码中测试第一行缺少XPATH\u PRODUCT\u DETAILS=“//table[@id='productDetails\u detailbollets\u sections1']”我的表内容:
。因此,我们建议
/table[@id='productDetails\u detailbounds\u sections1']/tbody/tr[normalize space(th)='Rank']/td//a/@href
如果
table
元素可能位于任何深度,则使用
//table[@id='productDetails\u detailbounds\u sections1']/tbody/tr[normalize space(th)='Rank']/td//a/@href
前提是
id
属性是唯一的。@Roshan:Alejandro向您展示了如何通过XPath直接到达目标。因此,您可以避免在python中对行进行迭代。@kjhughes是对的。我已经引用了这个问题的具体答案。@kjhughes。谢谢你澄清
/table/tbody/tr[normalize-space(th)='Rank']/td//a/@href
<table> 
  <tbody> 
    <tr> 
      <th class="a-color-secondary a-size-base prodDetSectionEntry">Product Number</th>  
      <td class="a-size-base">B003NR57BY</td> 
    </tr>  
    <tr> 
      <th class="a-color-secondary a-size-base prodDetSectionEntry">Rank</th>  
      <td> 
        <span> 
          <span>#3 in 
            <a href="/gp/bestsellers/pc/11036491/ref=pd_zg_hrsr_pc_1_1_last">Computer Mice</a>
          </span>  
          <br/> 
        </span> 
      </td> 
    </tr>  
    <tr> 
      <th class="a-color-secondary a-size-base prodDetSectionEntry">Created Date</th>  
      <td class="a-size-base">June 7, 2010</td> 
    </tr> 
  </tbody> 
</table>