Reputation: 541
New to python and scrapy, whats wrong with the code? I can retrieve the data from scrapy shell but why cannot retrieve in scrapy crawl?
EDIT: I have put the log at below, could you help me check what problem it is?
Problem SOLVED:
This link is incorrect ?price=2-2/
Should be ?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()
Log
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)
Upvotes: 0
Views: 277
Reputation: 217
If you want to extract the link for each article, i'd propose something like :
response.css("a.c-product-card::attr(href)")
instead of :
response.xpath('//a[contains(@class, "c-product-card__img-placeholder-inner")]/@href')
Upvotes: 2