使用XPATH提取表行

时间:2016-02-11 03:49:53

标签: xpath web-scraping scrapy scrapy-spider

我的html代码如下:

<TR ALIGN="LEFT">
<TD headers="usdot_number" ><center><font size="-1" face="Arial, Helvetica">1259247</FONT></center></TD>
<TD headers="prefix"><center><font size="-1" face="Arial, Helvetica">MC</FONT></center></TD>
<TD headers="docket_number" ><center><font size="-1" face="Arial, Helvetica">493001</FONT></center></TD>
<TD headers="legal_name" ><center><font size="-1" face="Arial, Helvetica"> E L ZAPATA TRANS INC</FONT></center></TD>
<TD headers="dba_name">&nbsp;</TD>
<TD headers="city" ><center><font size="-1" face="Arial, Helvetica">SPRING VALLEY</FONT></center></TD>
<TD headers="state" ><center><font size="-1" face="Arial, Helvetica">CA</FONT></center></TD>
<td headers="view_details"><center><font size="-1" face="Arial, Helvetica">
<BR>
<FORM ACTION="pkg_carrquery.prc_getdetail" METHOD="POST">
<INPUT TYPE="hidden" NAME="pv_apcant_id" VALUE="406294">
<INPUT TYPE="hidden" NAME="pv_vpath" VALUE="LIVIEW">
<input type="submit" value="HTML" onClick="">
</FORM>
</font></center></td>
<td headers="view_details"><center><font size="-1" face="Arial, Helvetica">
<BR>
<FORM ACTION="http://li-public.fmcsa.dot.gov/reports/rwservlet" METHOD="POST" name="reportForm" onSubmit="submitReportRequest(this.rptSummit,this)">
<INPUT TYPE="hidden" NAME="hidden_run_parameters" VALUE="lirpt">
<INPUT TYPE="hidden" NAME="report" VALUE="/u01/oracle/lirpts/li_carrier.rdf">
<INPUT TYPE="hidden" NAME="p_apcant" VALUE="406294">
<INPUT TYPE="hidden" NAME="p_user" VALUE="WEBLIVIEW">
<INPUT TYPE="submit" VALUE="Report" name="rptSummit">
</FORM>
</td>

我想从示例中提取每个TD的一些值(usdot_number,docket_number,dba_name和legal_name)以及pv_apcant_id值(406294)。我试着开始:

('//TABLE/TD headers/')

但没有奏效。我不知道如何处理TD[space]headers=value/表达式。有人可以帮我提一个建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

要访问XPath中的属性,您需要使用@字符。

以下是获取usdot_number文字的方法:

response.xpath("//td[@headers = 'usdot_number']/center/font").extract()

以下是提取pv_apcant_id值的示例表达式:

response.xpath("//input[@name = 'pv_apcant_id']/@value").extract()