使用Scrapy在管道内的MYSQL数据库中的2个表上添加项目

时间:2019-04-12 13:08:02

标签: python mysql web-scraping scrapy

嗨,我试图在我的MySQL数据库中添加2个表,我成功添加了2个表,但是未在数据库中添加诸如itemA和itemB之类的分隔项

如果我删除了itemB,则ItemA可以正常工作并添加到table1上,如果2个表始终出现此错误

“ NameError:名称'items_f21'未定义” 有任何想法吗?

这是我的代码

sales_item_spider.py

export const valueSearch = (
  value: string,
  hasEmptyValue: boolean,
  profile: ProfileHolder,
  attributeId: string,
  objectTypeId: string,
  typeName: string = "ValueSearch"
) => {
  const diff = hasValue
    ? {
      "value": value,
      "__typename": typeName
    }
    : {
      "hasValue": false,
      "__typename": "HasValue"
    };

  return {
    "__typename": "SearchFromProfile",
    "profileHolder": {
      "__typename": profile.__typename,
      "Id": profile.Id
    },
    "search": {
      "__typename": "HasValue",
      "objectTypeId": objectTypeId,
      "attributeId": attributeId,
      ...diff
    }
  };
};

pipelines.py

    def parse_1(self, response):

    item = GpdealsSpiderItem_hm()

    for product_item_hm in response.css('li.product-item'):

        hm_title = product_item_hm.css('h3.item-heading a.link::text').extract_first()
        hm_regular_price = product_item_hm.css('strong.item-price span.price.regular::text').extract_first()
        hm_sale_price = product_item_hm.css('strong.item-price span.price.sale::text').extract_first()
        hm_photo_url = product_item_hm.css('.image-container img::attr(data-src)').extract_first()
        hm_description_url = product_item_hm.css('h3.item-heading a::attr(href)').extract_first()

        item['hm_title'] = hm_title 
        item['hm_regular_price'] = hm_regular_price 
        item['hm_sale_price'] = hm_sale_price 
        item['hm_photo_url'] = hm_photo_url 
        item['hm_description_url'] = hm_description_url 

        yield item

def parse_2(self, response):

    items_f21 = GpdealsSpiderItem_f21()

    for product_item_forever in response.css('div.pi_container'):

        f21_title = product_item_forever.css('p.p_name::text').extract_first()
        f21_regular_price = product_item_forever.css('span.p_old_price::text').extract_first()
        f21_sale_price = product_item_forever.css('span.p_sale.t_pink::text').extract_first()
        f21_photo_url = product_item_forever.css('img::attr(data-original)').extract_first()
        f21_description_url = product_item_forever.css('a.item_slider.product_link::attr(href)').extract_first()

        items_f21['f21_title'] = f21_title 
        items_f21['f21_regular_price'] = f21_regular_price 
        items_f21['f21_sale_price'] = f21_sale_price 
        items_f21['f21_photo_url'] = f21_photo_url 
        items_f21['f21_description_url'] = f21_description_url 

        yield items_f21

1 个答案:

答案 0 :(得分:0)

您的store_db使用一个items_f21变量,该变量以前从未定义。

store_db上,您应仅使用item变量,并根据项目类型使用相应的INSERT语句:

def store_db(self, item):
    if isinstance(item, GpdealsSpiderItem_hm):
        self.curr.execute("""insert into saleitems_hm values (%s, %s, %s, %s, %s)""", (
                item['hm_title'],
                item['hm_regular_price'],
                item['hm_sale_price'],
                item['hm_photo_url'],
                item['hm_description_url']
            ))
    else:
        self.curr.execute("""insert into saleitems_f21 values (%s, %s, %s, %s, %s)""", (
                item['f21_title'],
                item['f21_regular_price'],
                item['f21_sale_price'],
                item['f21_photo_url'],
                item['f21_description_url']
            ))
    self.conn.commit()