我正试图从http://rotoguru1.com/cgi-bin/hyday.pl?game=fd抓取数据。但是下面的代码没有给我任何答案。我认为表路径或name变量的xpath有问题。有人可以帮助确定问题吗?
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
from kevin.items import RotoguruItem
from scrapy.selector import Selector, HtmlXPathSelector
class RotoguruSpider(CrawlSpider):
name = 'rotoguru1'
allowed_domains = ['rotoguru1.com']
start_urls = ['http://rotoguru1.com/cgi-bin/hyday.pl?']
rules = [Rule(LinkExtractor(allow=['mon=\d+&day=\d+&game=fd']), callback='parse_item')]
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
tablepath= '//table[@cellspacing="5"]/tbody//tr[position()>2]'
rows = hxs.select(tablepath)
items = []
for row in rows:
item = RotoguruItem()
item['name'] = row.select("td[2]/a/text()").extract()
items.append(item)
return items
上述代码的输出为
H:\SourceDir\basketball>scrapy crawl rotoguru1 -o fd.csv
2015-03-19 18:52:40+0530 [scrapy] INFO: Scrapy 0.24.5 started (bot: basketball)
2015-03-19 18:52:40+0530 [scrapy] INFO: Optional features available: ssl, http11
2015-03-19 18:52:40+0530 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE'
: 'basketball.spiders', 'FEED_FORMAT': 'csv', 'SPIDER_MODULES': ['basketball.spi
ders'], 'FEED_URI': 'fd.csv', 'BOT_NAME': 'basketball'}
2015-03-19 18:52:40+0530 [scrapy] INFO: Enabled extensions: FeedExporter, LogSta
ts, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2015-03-19 18:52:40+0530 [scrapy] INFO: Enabled downloader middlewares: HttpAuth
Middleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, Def
aultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, Redirec
tMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-03-19 18:52:40+0530 [scrapy] INFO: Enabled spider middlewares: HttpErrorMid
dleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddlew
are
2015-03-19 18:52:40+0530 [scrapy] INFO: Enabled item pipelines:
2015-03-19 18:52:40+0530 [rotoguru1] INFO: Spider opened
2015-03-19 18:52:40+0530 [rotoguru1] INFO: Crawled 0 pages (at 0 pages/min), scr
aped 0 items (at 0 items/min)
2015-03-19 18:52:40+0530 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6
023
2015-03-19 18:52:40+0530 [scrapy] DEBUG: Web service listening on 127.0.0.1:6080
2015-03-19 18:52:41+0530 [rotoguru1] DEBUG: Crawled (200) <GET http://rotoguru1.
com/cgi-bin/hyday.pl?game=fd> (referer: None)
2015-03-19 18:52:41+0530 [rotoguru1] INFO: Closing spider (finished)
2015-03-19 18:52:41+0530 [rotoguru1] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 236,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 17052,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2015, 3, 19, 13, 22, 41, 513000),
'log_count/DEBUG': 3,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2015, 3, 19, 13, 22, 40, 296000)}
2015-03-19 18:52:41+0530 [rotoguru1] INFO: Spider closed (finished)
提前感谢您的帮助。
答案 0 :(得分:1)
有两个问题:
1)start_urls的网址并非使用game=fd
,但LinkExtractor期望其网址为game=fd
。
2)用于表的XPath使用tbody
,但这在Scrapy解析的HTML源代码中不可用(即使此XPath表达式在浏览器中有效,因为浏览器会自动添加tbody)。 / p>
在start_urls中添加game=fd
,将表路径设为//table[@cellspacing="5"]//tr[position()>2]
,它应该可以正常工作。