python和scrapy的新手,代码有什么问题?我可以从scrapy shell中检索数据,但为什么无法在scrapy抓取中检索?
编辑:我把日志放在下面,你能帮我查一下它是什么问题吗?问题已解决:
这个链接不正确?price = 2-2 /
应该是?price = 2-2
quotes.spider.py
import scrapy
from tutorial.items import TutorialItem
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'http://www.testing.com.my/?price=2-2/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
titles = response.xpath('//a[contains(@class, "c-product-card__img-placeholder-inner")]/@href')
return titles.extract()
items.py
from scrapy.item import Item, Field
class TutorialItem(Item):
link = Field()
日志
2017-07-05 16:26:52 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: tutorial
)
2017-07-05 16:26:52 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': '
tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'ROBOTSTXT_OBEY': True, 'SPID
ER_MODULES': ['tutorial.spiders']}
2017-07-05 16:26:52 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2017-07-05 16:26:52 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-07-05 16:26:52 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-07-05 16:26:52 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-07-05 16:26:52 [scrapy.core.engine] INFO: Spider opened
2017-07-05 16:26:52 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pag
es/min), scraped 0 items (at 0 items/min)
2017-07-05 16:26:52 [scrapy.extensions.telnet] DEBUG: Telnet console listening o
n 127.0.0.1:6023
2017-07-05 16:26:55 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.la
zada.com.my/robots.txt> (referer: None)
2017-07-05 16:26:55 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.la
zada.com.my/shop-power-banks2/?price=2-2/> (referer: None)
2017-07-05 16:26:55 [scrapy.core.engine] INFO: Closing spider (finished)
2017-07-05 16:26:55 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 596,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 55953,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 7, 5, 8, 26, 55, 942385),
'log_count/DEBUG': 3,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2017, 7, 5, 8, 26, 52, 624796)}
2017-07-05 16:26:55 [scrapy.core.engine] INFO: Spider closed (finished)
答案 0 :(得分:2)
如果你想提取每篇文章的链接,我建议像:
response.css("a.c-product-card::attr(href)")
而不是:
response.xpath('//a[contains(@class, "c-product-card__img-placeholder-inner")]/@href')