如果我的 html 包含一个表格,其中列代表字段,行代表记录,但第一列中的某些单元格只是文本,有些是链接,我怎样才能让这些都进入正确的字段?我只能通过省略第一列来考虑如何做到这一点,并且 .
例如:
<tbody>
<tr id="ps_134922471">
<td><a href="/114911935">184A Kent St</a></td>
<td class="center">House</td>
<td class="currency price">$600,000</td>
<td>Auction</td>
<td class="center bed">4</td>
<td class="date">19/10/13</td>
</tr>
<tr id="ps_134922515">
<td>5/189 Rockingham Beach Rd</td>
<td class="center">Unit</td>
<td class="currency price">$502,000</td>
<td>Normal Sale</td>
<td class="center bed">3</td>
<td class="date">10/09/13</td>
</tr>
等等……等等……
我讨厌的解决方案省略了第一列(恰好是街道地址):
def parse(self, response):
hxs = HtmlXPathSelector(response)
items = []
item = SoldItem()
types = hxs.select('//table[@id="recentSales"]/tbody/tr/td[@class="center"]/text()').extract()
beds = hxs.select('//table[@id="recentSales"]/tbody/tr/td[@class="center bed"]/text()').extract()
prices = hxs.select('//table[@id="recentSales"]/tbody/tr/td[@class="currency price"]/text()').extract()
dates = hxs.select('//table[@id="recentSales"]/tbody/tr/td[@class="date"]/text()').extract()
for i in range(len(types)):
item['type'] = types[i]
item['bed'] = beds[i]
item['price'] = prices[i]
item['saledate'] = dates[i]
items.append(item)
return items
pass
任何帮助表示赞赏。谢谢