1

这是来自网站的源代码:http ://www.example.com ,我想用 scrapy 爬虫提取所有 THIS IS A TEXT。

<tr>
<td>
<table>
   <tr>
       <td colspan="5" style="text-align:left;padding-left:4px;" class="category">  <imgsrc="http://www.example.com/images/menu.gif"> 
        THIS IS A TEXT </td>
   </tr>
                               <tr>
       <td class="date" colspan="5">THIS IS A TEXT</td>
   </tr>
                               <tr>
       <td style="test-align:left;width:40px;">THIS IS A TEXT</td>
       <td style="padding-right:4px; width:180px;text-align:right">
       THIS IS A TEXT </td>
                                       <td style="width:40px;text-align:center"> <nobr><a id="I1" name="I1"
href="javascript:MoreInformation(1,'1141','1563513','TT','home');">
       THIS IS A TEXT</a></nobr>
        </td>
       <td style="padding-left:5px; width:180px;text-align:left">
       THIS IS A TEXT </td>
       <td style="width:40px;text-align:center"></td>
   </tr>
                               <tr>
       <td style="test-align:left;width:40px;">THIS IS A TEXT </td>
       <td style="padding-right:4px; width:180px;text-align:right">
       THIS IS A TEXT </td>
                                       <td style="width:40px;text-align:center"> THIS IS A TEXT  </td>
       <td style="padding-left:5px; width:180px;text-align:left">
       THIS IS A TEXT </td>
       <td style="width:40px;text-align:center"></td>
   </tr>*
</table>
</td>
</tr>

这是我的 scrapy_project.py:我试图从 td:rows = hxs.select('.//td') 中提取所有内容,但我不知道如何提取单独的“这是一个文本”。我收到这个错误:u'\n\t\t\t\t\t\t\t\t。有人可以帮助我吗?

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from dirbot.items import Website

class DmozSpider(BaseSpider):
    name = "dmoz"
    allowed_domains = ["example.com"]
    start_urls = [
        "http://www.example.com/",
        "",
    ]

    def parse(self, response):

        hxs = HtmlXPathSelector(response)
        rows = hxs.select('//table[@id="content"]//table/tr')
        items = []

        for row in rows:
            item = Website()
            item ["job"] = row.select("td[1]/text()").extract()
            item ["description"] = row.select("td[0]/a/nobr/text()").extract()
            item ["name"] = row.select("td[2]/text()").extract()
            items.append(item)

        return items

另一个问题:如何消除这个:u'\n\t\t\t\t\t\t\t\t

4

0 回答 0