无法使用scrapy获取HTML表格中的行数。
我有一个如下所示的HTML文档:
<TITLE>Title of Mbean</TITLE>
</HEAD>
<BODY>
<TABLE WIDTH="100%">
<TR>
<TD ALIGN="left"><H2>Array View</H2></TD>
<TD ALIGN="right" VALIGN="top">[JDMK5.0_r16]</TD>
</TR></TABLE>
<UL type="disc"><LI><B>MBean Name:</B> Name of MBean</LI>
<LI><B>MBean Attribute:</B> product list</LI>
<LI><B>Array of:</B> java.lang.String</LI>
</UL>
<TABLE WIDTH="100%"><TR>
<TD ALIGN="LEFT"><A HREF="Object name">Back to MBean View</A></TD>
<TD ALIGN="RIGHT"><A HREF="/">Back to Agent View</A></TD>
</TR></TABLE>
<HR>
<A NAME="Top"></A>
<TABLE ALIGN="center" BORDER="1" CELLPADDING="3">
<TR>
<TH WIDTH="10%"> Element at </TH>
<TH WIDTH="7%"> Access </TH>
<TH WIDTH="35%"> Value </TH>
</TR>
<TR>
<TD ALIGN="center">0</TD><TD ALIGN="center">RO</TD>
<TD>this is the value</TD>
</TR>
<TR>
<TD ALIGN="center">0</TD><TD ALIGN="center">RO</TD>
<TD>this is the second value</TD>
</TR>
</TABLE>
</HR>
</BODY>
</HTML>
当我指定以下内容使用scrapy使用以下xpath尝试并计算表中的行数时,不会返回任何内容:
hxs = Selector(response)
loader = XPathItemLoader(WebscraperItem(), selector=hxs)
loader.add_xpath('tablecount', 'count(/HTML/BODY/HR/TABLE/TR)')
yield loader.load_item()
我在Oxygen中尝试了xpath,并且可以看到xpath工作正常,但是不在scrapy中,有没有人知道我哪里出错?
答案 0 :(得分:3)
我已将您的html复制到名为in.html的本地文件中:
$ scrapy shell in.html
...
>>> from scrapy.item import Item, Field
>>> class WebscraperItem(Item): tablecount = Field()
...
>>> from scrapy.contrib.loader import XPathItemLoader
>>> loader = XPathItemLoader(WebscraperItem(), selector=sel)
>>> loader.add_xpath('tablecount', 'count(//table/tr)')
>>> loader.load_item()
{'tablecount': [u'5.0']}