【问题标题】:Scrapy - crawled (200) and referer : noneScrapy - 爬取(200)和引用:无
【发布时间】:2017-11-22 20:14:07
【问题描述】:

我正在尝试学习如何使用 scrapy 和 python,但我根本不是专家……离这里很远。 爬完这个页面后我总是有一个空文件:product of c-discount,我不明白为什么......

这是我的代码:

import scrapy

from cdiscount_test.items import CdiscountTestItem

f = open('items.csv', 'w').close()

class CdiscountsellersspiderSpider(scrapy.Spider):
    name = 'CDiscountSellersSpider'
    allowed_domains = ['cdiscount.com']
    start_urls = ['http://www.cdiscount.com/mpv-8732-SATENCO.html']

    def parse(self, response):
        items = CdiscountTestItem()
        name = response.xpath('//div[@class="shtName"]/div[@class="shtOver"]/h1[@itemprop="name"]/text()').extract()
        country = response.xpath('//div[@class="shtName"]/span[@class="shTopCExp"]/text()').extract()

        items['name_seller'] = ''.join(name).strip()
        items['country_seller'] = ''.join(country).strip()
        pass

我在 cmd 窗口中得到的结果:

2017-06-20 18:01:50 [scrapy.core.engine] INFO: Spider opened
2017-06-20 18:01:50 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 
pages/min), scraped 0 items (at 0 items/min)
2017-06-20 18:01:50 [scrapy.extensions.telnet] DEBUG: Telnet console 
listening on 127.0.0.1:6023
2017-06-20 18:01:51 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
http://www.cdiscount.com/robots.txt> (referer: None)
2017-06-20 18:01:51 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
http://www.cdiscount.com/mpv-8732-SATENCO.html> (referer: None)
2017-06-20 18:01:51 [scrapy.core.engine] INFO: Closing spider (finished)

请问有人帮我吗?

非常感谢!!!

【问题讨论】:

    标签: python scrapy referer


    【解决方案1】:

    同一问题的一种可能情况是网站内容是动态生成的。您可以通过访问网站并点击查看页面源来检查。 在这种情况下,您可能必须将 splash 与 scrapy 一起使用。

    【讨论】:

      【解决方案2】:

      这里的主要问题是您没有将 parse 方法中的项目传递回 Scrapy 引擎。您在parse 中的最后一个命令是pass,因此您只需丢弃该项目。相反,您需要使用 yield item 将项目从蜘蛛传递到 Scrapy 引擎以进行进一步处理。

      【讨论】:

      • 好的,我的错...谢谢!我正在寻找一些复杂的东西,其中只有一个收益是有用的......
      • 你知道'.re()'吗?我看到类似的东西只保留 en 提取文本的一部分,但我不知道要放入 () 中的 args 是什么
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2020-02-05
      相关资源
      最近更新 更多