【问题标题】:Scrapy not returning all the items it shouldScrapy没有返回它应该返回的所有项目
【发布时间】:2015-07-03 09:46:26
【问题描述】:

我试图让 Scrapy 爬过一个网站,但只将它限制在与特定模式匹配的页面上,这让我很头疼。

网站结构如下:

website.com/category/page1/
website.com/category/page2/
website.com/category/page3/

等等。

我需要它从 category 开始爬取,然后跟随所有指向另一个页面的链接(总共有 375 个页面,当然数量不固定)。

问题是它在我停止它之前爬了大约 10 页,但它只返回 10-15 个项目,其中应该有 200+

这是我的代码,它不能正常工作:

class WSSpider(CrawlSpider):
name = "ws"
allowed_domains = ["website.com"]
start_urls = ["https://www.website.com/category/"]
rules = (
    Rule(LinkExtractor(allow=("/level_one/page*",)), callback="parse_product", follow=True),
)

    def parse_product(self, response):
        sel = Selector(response)
        sites = sel.css(".pb-infos")
        items = []

        for site in sites:
            item = Website()
            item["brand"] = site.css(".pb-name .pb-mname::text").extract()
            item["referinta"] = site.css(".pb-name a::text").extract()
            item["disponibilitate"] = site.css(".pb-availability::text").extract()
            item["pret_vechi"] = site.css(".pb-sell .pb-old::text").extract()
            item["pret"] = site.css(".pb-sell .pb-price::text").extract()
            item["procent"] = site.css(".pb-sell .pb-savings::text").extract()
            items.append(item)

        #return items
        f = open("output.csv", "w")
        for item in items:
            line = \
                item["brand"][0].strip(), ";", \
                item["referinta"][-1].strip(), ";", \
                item["disponibilitate"][0].strip(), ";", \
                item["pret_vechi"][0].strip().strip(" lei"), ";", \
                item["pret"][0].strip().strip(" lei"), ";", \
                item["procent"][0].strip().strip("Mai ieftin cu "), "\n"
            f.write("".join(line))
        f.close()

非常感谢任何帮助!

【问题讨论】:

    标签: python web web-crawler scrapy depth


    【解决方案1】:

    我发现了我的(愚蠢的)错误。

    f = open("output.csv", "w")
    

    实际上应该是

    f = open("output.csv", "a")
    

    【讨论】:

      【解决方案2】:

      我曾经写了一个 python 爬虫来在一个内部 wiki 站点关闭之前下载它 - 遇到了一个问题,即我们的 Intranet 或 wiki 服务器正在限制我的脚本对内容的访问。我认为有一种方法可以让 scrapy 访问得更慢。

      我遇到的另一个问题是身份验证 - wiki 的某些部分需要登录才能阅读。

      另一个问题是你每次都覆盖 output.csv...

      【讨论】:

        【解决方案3】:

        parse_product 是异步的,请改用CsvItemExporterhttp://doc.scrapy.org/en/latest/topics/exporters.html#csvitemexporter

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-27
          • 1970-01-01
          • 1970-01-01
          • 2022-11-03
          • 2022-11-13
          • 2019-07-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多