隨着我在這裏寫代碼的方式我從不同的網站得到的結果,但由於某種原因,這個網站拋出錯誤。由於我是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項目設置?此外,請確保您發佈的回溯和錯誤是文本,而不是截圖(不可搜索)。謝謝。 – alecxe
謝謝先生,謝謝你的回覆。我在settings.py中沒有做任何事情,而是保持原樣。無論如何,在描述中也加上這一點。 – SIM