chan go
chan go

Reputation: 137

extract table rows with XPATH

I have html code that look like:

<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>

I want to extract some values of each TD (usdot_number, docket_number, dba_name and legal_name) and the pv_apcant_id value (406294) from the example. I tried to begin with:

('//TABLE/TD headers/')

But didn't work. I don't know how deal with the TD[space]headers=value/ expression. Anyone can please help me with a suggestion?

Thanks!

Upvotes: 0

Views: 463

Answers (1)

alecxe
alecxe

Reputation: 473873

To access attribute in XPaths, you need to use the @ character.

Here is how you can get the usdot_number text:

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

Here is a sample expression to extract the pv_apcant_id value:

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

Upvotes: 1

Related Questions