【问题标题】:Scrapy is done running with results in the console but CSV output remains blankScrapy 已在控制台中运行结果,但 CSV 输出保持空白
【发布时间】:2017-04-01 22:23:01
【问题描述】:

我对scrapy很陌生,所以如果在csv文件中没有结果,我很难找出我做错了什么。我可以在控制台中看到结果。这是我尝试过的:

主文件夹名为“realyp”。 蜘蛛文件名为“yp.py”,代码:

from scrapy.selector import Selector
from scrapy.spider import BaseSpider
from realyp.items import RealypItem

class MySpider(BaseSpider):
     name="YellowPage"
     allowed_domains=["yellowpages.com"]
     start_urls=["https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=Los%20Angeles%2C%20CA&page=2"]

     def parse(self, response):
        title = Selector(response)
        page=title.xpath('//div[@class="info"]')
        items = []
        for titles in page:
            item = RealypItem()
            item["name"] = titles.xpath('.//span[@itemprop="name"]/text()').extract()
            item["address"] = titles.xpath('.//span[@itemprop="streetAddress" and @class="street-address"]/text()').extract()
            item["phone"] = titles.xpath('.//div[@itemprop="telephone" and @class="phones phone primary"]/text()').extract()
            items.append(item)
        return items

“items.py”文件包括:

from scrapy.item import Item, Field
class RealypItem(Item):
    name= Field()
    address = Field()
    phone= Field()

要获得 csv 输出,我的命令行是:

cd desktop
cd realyp
scrapy crawl YellowPage -o items.csv -t csv

任何帮助将不胜感激。

【问题讨论】:

  • 您能否更准确地描述您的预期以及与您的结果有何不同?将其表述为问题也将有所帮助。文件 test.py 是如何使用的?我只看到 items.py 是如何从命令行使用的
  • 感谢您的回复,MrJLP。实际上,我希望第一列中出现“项目标题”,第二列中出现“项目链接”,但作为 csv 输出,我只得到没有结果的标题名称。
  • 我不熟悉scrapy。要调试,您可以打印出“title”和“item”的值以查看断开连接的位置。
  • 我已经测试了您的代码,它会为我将结果写入文件中。在您的情况下,文件 items.csv 保持为空还是根本没有生成?
  • 感谢托马克先生的回复。它确实会生成 csv 文件,但除了标题之外没有任何输出。

标签: python csv scrapy-spider


【解决方案1】:

正如@Granitosauros 所说,您应该使用yield 而不是return。产量应该在 for 循环内。 在 for 循环中,如果路径以 // 开头,则选择文档中满足以下条件的所有元素(参见 here)。

这是一个适合我的(粗略的)代码:

# -*- coding: utf-8 -*-
from scrapy.selector import Selector
from scrapy.spider import BaseSpider
from realyp.items import RealypItem

class MySpider(BaseSpider):
    name="YellowPage"
    allowed_domains=["yellowpages.com"]
    start_urls=["https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=Los%20Angeles%2C%20CA&page=2"]

    def parse(self, response):
        for titles in response.xpath('//div[@class = "result"]/div'):
            item = RealypItem()
            item["name"] = titles.xpath('div[2]/div[2]/h2 /a/span[@itemprop="name"]/text()').extract()
            item["address"] = titles.xpath('string(div[2]/div[2]/div/p[@itemprop="address"])').extract()
            item["phone"] = titles.xpath('div[2]/div[2]/div/div[@itemprop="telephone" and @class="phones phone primary"]/text()').extract()
            yield item

【讨论】:

  • 我刚刚注意到我在代码中的错误。在为“name”、“address”和“phone”定义 xpath 表达式时,我从“titles”元素中获取了它,但我忘记使用点运算符 (.//) 并错误地使用了 (//)。我已经更正了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
相关资源
最近更新 更多