7

Scrape data from a table with scrapy. The table html is like:

<table class="tablehd">

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

Entire list is avialable by

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

How would you split them to each item and then assign data td1 - td5ta

4

2 回答 2

12

不知道你想在你的项目中看到什么,但这里有一个例子,我希望就是这样:

class MyItem(Item):
    value = Field()


class MySpider(BaseSpider):
    ...

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        items = hxs.select('//table[@class="tablehd"]/td')

        for item in items:
            my_item = MyItem()
            my_item['value'] = item.select('.//text()').extract()
            yield my_item

希望有帮助。

于 2013-07-02T19:43:26.240 回答
0

当您说“将它们分成每个项目”时,您是指每个班级/行吗?

无论如何,我这样做的方式就是使用正则表达式。

import urllib, re
html=urllib.urlopen('domain.com')
itemfinder=re.compile('td>(.*)</td>')
items=re.findall(itemfinder, html)

如果要按行拆分,则:

rowfinder=('tr', re.Multiline)
rows=re.findall(rowfinder, html)
for row in rows:
    ...code above except substitute variables apropos
于 2013-07-02T19:45:51.187 回答