【问题标题】:Scrapy Crawling 0 pagesScrapy Crawling 0 页
【发布时间】:2017-07-05 17:40:26
【问题描述】:

python和scrapy的新手,代码有什么问题?我可以从scrapy shell中检索数据,但为什么不能在scrapy crawl中检索?

编辑:我把日志放在下面,你能帮我看看是什么问题吗?

问题已解决:
此链接不正确?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)

【问题讨论】:

  • 是缩进的问题吗?与类定义同级的方法定义
  • 你能分享你的scrapy crawl日志吗?
  • @Pablo 缩进不应该是我这边的问题,我在这里复制并粘贴它,但对齐有一些问题,所以我手动缩进它
  • 我认为选择器是错误的,没有得到任何东西,这就是为什么你没有得到任何输出。我在网站上试过,但链接的类名没有返回任何内容。你想提取什么?
  • @Pablo 是的,我正在尝试提取,我将编辑我想要提取的内容

标签: python html scrapy


【解决方案1】:

如果您想提取每篇文章的链接,我会提出类似的建议:

response.css("a.c-product-card::attr(href)")

而不是:

response.xpath('//a[contains(@class, "c-product-card__img-placeholder-inner")]/@href')

【讨论】:

  • 不错的选择,反正我已经解决了之前的问题,谢谢你的帮助
猜你喜欢
  • 2017-02-28
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多