编辑:这已经解决了!XPATH 是问题所在。
我很困惑。我正在尝试编写一个非常简单的蜘蛛,以抓取网站(talkbass.com)以获取分类低音部分中所有链接的列表(http://www.talkbass.com/forum/f126/) . 我根据教程(我相对轻松地完成了)编写了蜘蛛,而这个只是不工作。我可能做错了很多,因为我也尝试合并规则,但我什么也没得到。
我的商品代码是:
from scrapy.item import Item, Field
class BassItem(Item):
title = Field()
link = Field()
print title, link
我的蜘蛛代码是:
from scrapy.spider import BaseSpider
from scrapy.contrib.spiders import Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from tutorial.items import BassItem
class BassSpider(BaseSpider):
name = "bass"
allowed_domains = ["www.talkbass.com"]
start_urls = ["http://www.talkbass.com/forum/f126/"]
rules = (
# Extract links matching 'f126/xxx'
# and follow links from them (since no callback means follow=True by default).
Rule(SgmlLinkExtractor(allow=('/f126/(\d*)/', ), ))
)
def parse(self, response):
hxs = HtmlXPathSelector(response)
ads = hxs.select('/html/body/div/div/div/table/tbody/tr/td/form/table/tbody')
items = []
for ad in ads:
item = BassItem()
item['title'] = ad.select('a/text()').extract()
item['link'] = ad.select('a/@href').extract()
items.append(item)
return items
我没有收到任何错误,但日志没有返回任何内容。这是我在控制台中看到的:
C:\Python27\Scrapy\tutorial>scrapy crawl bass
2013-01-07 14:36:49+0800 [scrapy] INFO: Scrapy 0.16.3 started (bot: tutorial)
2013-01-07 14:36:49+0800 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, Co
reStats, SpiderState
{} {}
2013-01-07 14:36:51+0800 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddl
eware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, Htt
pCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-01-07 14:36:51+0800 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, Refe
rerMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-01-07 14:36:51+0800 [scrapy] DEBUG: Enabled item pipelines:
2013-01-07 14:36:51+0800 [bass] INFO: Spider opened
2013-01-07 14:36:51+0800 [bass] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-01-07 14:36:51+0800 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-01-07 14:36:51+0800 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-01-07 14:36:52+0800 [bass] DEBUG: Crawled (200) <GET http://www.talkbass.com/forum/f126/> (referer: None)
2013-01-07 14:36:52+0800 [bass] INFO: Closing spider (finished)
2013-01-07 14:36:52+0800 [bass] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 233,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 17997,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2013, 1, 7, 6, 36, 52, 305000),
'log_count/DEBUG': 7,
'log_count/INFO': 4,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2013, 1, 7, 6, 36, 51, 458000)}
2013-01-07 14:36:52+0800 [bass] INFO: Spider closed (finished)
我从来没有做过一个实际的项目,我认为这是一个很好的起点,但我似乎无法解决这个问题。
我也不确定 XPath 是否正确。我正在使用一个名为 XPath helper 的 chrome 扩展。我需要的部分之一是这样的:
/html/body/div[1]/div[@class='page']/div/table[5]/tbody/tr/td[2]/form[@id='inlinemodform']/table[@id='threadslist']/tbody[@id='threadbitsforum126']/tr[6]/td[@id='td_threadtitle944468']/div[1]/a[@id='thread_title944468']
但是,如果您看到“ tr[6] ”和“944468” - 对于每个链接来说,它们并不是恒定的(其他都是)。我刚刚删除了类名和数字,这让我看到了你在我的蜘蛛代码中看到的内容。
还只是添加 - 当我直接从 XPath Helper 复制和粘贴 XPath 时,它会给出语法错误:
ads = hxs.select('/html/body/div[1]/div[@class='page']/div/table[5]/tbody/tr/td[2]/form[@id='inlinemodform']/table[@id='threadslist']/tbody[@id='threadbits_forum_126']/tr[6]/td[@id='td_threadtitle_944468']/div[1]/a[@id='thread_title_944468']')
^
SyntaxError: invalid syntax
我已经尝试过解决这个问题(使用元素不恒定的通配符)并且每次尝试时都收到语法错误