【问题标题】:Table needs to be scraped with scrapy [duplicate]表需要用scrapy刮[重复]
【发布时间】:2013-07-02 17:21:06
【问题描述】:

有一张桌子需要用scrapy刮。数据格式如下:

<table>

<tr class="colhead">
<td width="170">MON, NOV 11</td>
<td width="80">Item</td>
<td width="60" align="center"></td>
<td width="210">Item</td>
<td width="220">Item</td>
</tr>

<tr class="oddrow">
<td> Item </a></td>
<td> Item </td>
<td align="center"> Item </td>
<td></td>
<td> Item </td>
</tr>

<tr class="evenrow">
<td> Item </a></td>
<td> Item </td>
<td align="center"> Item </td>
<td></td>
<td> Item </td>
</tr>


</table>

我确实得到了完整的项目列表

items = hxs.select('//table[@class="tablehd"]//td//text()').extract()

如何将它们拆分为每个项目,然后分配数据 td1 - td5

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    基于tutorial shell 示例,您应该首先获取&lt;tr&gt; 元素,然后从中获取&lt;td&gt;,如下所示:

    rows = hxs.select('//tr')
    for row in rows:
        print row.select('td/text()').extract()
    

    rows 将是您迭代的HtmlXPathSelector 对象列表,然后从每个&lt;td&gt; 中提取文本以获取当前&lt;tr&gt;

    row.select('td/text()').extract() 将是一个列表,其中包含给定行的每个单元格的文本:

    [u'MON, NOV 11', u'Item', u'Item', u'Item']
    [u' Item ', u' Item ', u' Item ', u' Item ']
    [u' Item ', u' Item ', u' Item ', u' Item ']
    

    【讨论】:

    • 谢谢..它现在可以工作了..必须替换行..
    猜你喜欢
    • 2020-07-28
    • 2014-07-16
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多