簡體   English   中英

cra草不退還應該的所有物品

[英]Scrapy not returning all the items it should

我試圖讓Scrapy爬過一個網站,但僅將其限制在匹配特定模式的頁面上,這讓我很頭疼。

該網站的結構如下:

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

等等。

我需要它來開始從類別中進行爬網,然后跟蹤通往另一頁的所有鏈接(共有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()

任何幫助深表感謝!

我發現了(愚蠢的)錯誤。

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

實際上應該是

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

我曾經寫過一個Python抓取工具,用於在關閉內部Wiki網站之前下載它-遇到了一個問題,即我們的Intranet或Wiki服務器正在限制我的腳本對內容的訪問。 我認為有一種方法可以告訴scrapy訪問速度較慢。

我遇到的另一個問題是身份驗證-Wiki的某些部分需要登錄才能讀取。

另一個問題是您每次都覆蓋output.csv ...

parse_product是異步的,請改用CsvItemExporter http://doc.scrapy.org/en/latest/topics/exporters.html#csvitemexporter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM