0
这是一个来自网站的源代码:http://www.example.com,我想用scrapy抓取工具提取所有这是一个文本。用Scrapy提取单独的文本
<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:行数= 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
_我不知道如何提取单独的“这是一个文本”_对我来说是不清楚的。话虽如此,'td [0]/a/nobr/text()'不会匹配任何内容,因为XPath中的位置以'1'开头。 'u'\ n \ t \ t \ t \ t \ t \ t \ t'仅仅是引导选择器提取的空白。您可以使用'.strip()'方法删除前导和尾部的空白字符。如果可以的话,你应该为'TD'使用更多的acurate选择器,比如'td [@ class =“category”]'或'td [@ class =“date”]' –
我试试:rows = hxs。 select'('// td [@ class =“category”]/a/@ href')但我收到错误:'raise ValueError('请求url中缺少方案:%s'%self._url) exceptions.ValueError:请求URL中缺少方案:'有人可以告诉我哪里错了吗? – Floriano
你可以发布你的堆栈跟踪/控制台日志吗? (最好是一些pastebin服务)什么代码行会产生这个错误?你是否生成了“请求”? –