2017-04-14 77 views
1

隨着我在這裏寫代碼的方式我從不同的網站得到的結果,但由於某種原因,這個網站拋出錯誤。由於我是scrapy的新編碼員,因此我無法自行解決問題。 Xpaths是好的。我附加了我在終端看到的代碼一起:Scrapy引發屬性錯誤

items.py

import scrapy 
class OlxItem(scrapy.Item): 
    Title = scrapy.Field() 
    Url = scrapy.Field() 

olxsp.py

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.linkextractors import LinkExtractor 

class OlxspSpider(CrawlSpider): 
    name = "olxsp" 
    allowed_domains = ['olx.com.pk'] 
    start_urls = ['https://www.olx.com.pk/'] 

    rules = [Rule(LinkExtractor(restrict_xpaths='//div[@class="lheight16 rel homeIconHeight"]')), 
      Rule(LinkExtractor(restrict_xpaths='//li[@class="fleft tcenter"]'), 
      callback='parse_items', follow=True)] 

    def parse_items(self, response): 
     page=response.xpath('//h3[@class="large lheight20 margintop10"]') 
     for post in page: 
      AA=post.xpath('.//a[@class="marginright5 link linkWithHash detailsLink"]/span/text()').extract() 
      CC=post.xpath('.//a[@class="marginright5 link linkWithHash detailsLink"]/@href').extract() 
      yield {'Title':AA,'Url':CC} 

settings.py

BOT_NAME = 'olx' 
SPIDER_MODULES = ['olx.spiders'] 
NEWSPIDER_MODULE = 'olx.spiders' 

ROBOTSTXT_OBEY = True 

圖像終端在scrapy完成運行後: enter image description here

+0

你還可以發佈你的Scrapy項目設置?此外,請確保您發佈的回溯和錯誤是文本,而不是截圖(不可搜索)。謝謝。 – alecxe

+0

謝謝先生,謝謝你的回覆。我在settings.py中沒有做任何事情,而是保持原樣。無論如何,在描述中也加上這一點。 – SIM

回答

1
  1. 您有ROBOTSTXT_OBEY = True,它告訴scrapy檢查它所搜尋域的robots.txt文件,以便確定如何對這些網站進行禮貌。

  2. 您允許allowed_domains = ['www.olx.com']中的一個不同於您實際爬網的域名。如果您只想抓取olx.com.pk網站,請將allowed_domains更改爲['olx.com.pk']。如果您實際上不知道要抓取哪些網站,請刪除allowed_domains屬性。

+0

感謝eLRuLL,感謝您的迴應。根據你的建議滿足這兩個參數也不會帶來任何結果。 – SIM

+0

那麼新的錯誤是什麼? – eLRuLL

+0

對不起,我的電腦。現在,它顯示「包裝的請求對象沒有屬性類型,屬性錯誤14. – SIM