我在这里做错了什么?parse_item_page 对我不起作用

import scrapy

from ..items import dealItem


class FarmtoolsSpider(scrapy.Spider):

    name = 'farmtools'

    allowed_domains = ['www.deal.ie']

    start_urls = ['https://www.deal.ie/all?source=private&sort=publishdate%20desc']


    def parse(self, response):

        items = dealItem()

        rows = response.xpath('//ul[@class="card-collection"]/li')


        for row in rows:

            link = row.xpath('.//a/@href').get() #this is the full link.

            link_split = link.split('/')[-1] #this splits the url link th first time.

            linkid = link_split.split('?')[0] #this splits it the second time.

            title = row.xpath('.//div[1]/p[@class="card__body-title"]/text()').get()

            county = row.xpath('.//a/div/div[2]/div[1]/ul[@class="card__body-keyinfo"]/li[contains(text(),"min")]/following-sibling::node()/text()').get()

            price = row.xpath('.//p[@class="card__price"]/span[1]/text()').get()

            subcat = row.xpath('.//a/div/div[2]/div[1]/p[2]/text()[2]').get()

            zero = row.xpath('.//a/div/div[2]/div[1]/ul[@class="card__body-keyinfo"]/li[contains(text(),"min")]/text()').get()

            if zero == '0 min':


                items['linkid'] = linkid

                items['title'] = title

                items['county'] =  county

                items['price'] = price

                items['subcat'] = subcat

                items['zero'] = zero

            }

第一个解析方法运行良好,但我不确定我是否正确编写了第二个解析方法。


第一个解析方法的一部分会抓取我想要跟踪的特定链接,这样我就可以从中抓取额外的数据。


我想要实现的是抓取我从第一个解析方法获得的链接并从中获取其他项目值,然后将它们全部添加为项目。


我也添加了items = DonedealItem()两者,但我不确定这是正确的。


当我现在运行它时,我得到:文件“/home/william/.local/lib/python3.7/site-packages/twisted/internet/defer.py”,第 654 行,在 _runCallbacks current.result = callback(current. result, *args, **kw) TypeError: parse_item_page() got an unexpected keyword argument 'donedeal.ie/cars-for-sale/e39-520i-for-breaking-also-have-lsd/…' 。对于每个被抓取的链接,我都会得到其中一个。我想要做的是向我从第一个解析方法中抓取的信息添加更多信息,如“卖家 ID”、“视图”等。希望这可以帮助



元芳怎么了
浏览 163回答 1
1回答

有只小跳蛙

我可以发现您的代码存在一些问题,包括您在评论中提到的错误原因,但问题仍然存在,因为我不完全理解您想要抓取的数据结构。在您的parse方法中,您实例化了varDonedealItem()中的一个对象items。然后您尝试将其作为回调参数 (cb_kwargs) 发送给回调函数 ( parse_item_page)。      ...      yield response.follow(url=link,          callback=self.parse_item_page,          cb_kwargs={link: items}) def parse_item_page(self, response, link):     ...这里的问题是,这link是一个具有不可预测值的变量(因为您将其设置为 url),它需要是一个与您在回调函数中定义的值相同的字符串。所以这应该可以解决错误:        yield response.follow(url=link,            callback=self.parse_item_page,            cb_kwargs={'link': items})但是,您的parse_item_page方法没有link任何用途。相反,它实例化一个新的DonedealItem(),用解析到这个方法中的数据填充它,然后产生它。作为参数接收的数据link未被使用。如果你想增加你第一个方法发送给你的数据parse,你需要使用它:  def parse_item_page(self, response, link):      items = link # Your link param already receives an DonedealItem instance.这是一个提示:不需要这些花括号: yield{     items     }改用yield items_仍然存在的问题:你可以把你想象DonedealItem成一个字典,你的代码正在做的是首先实例化项目,然后运行一个 for 循环,并在这个循环的每次迭代中覆盖你的项目中的数据。我相信你应该在 for 循环中实例化你的项目,但我不能确定,因为我不知道预期的结果是什么,也不知道是如何DonedealItem定义的。即便如此,这仍然会导致第二种解析方法出现问题,因为它接收到一个项目并再次迭代。也许你需要使用列表。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python