【发布时间】:2020-06-07 13:42:27
【问题描述】:
我正在使用 scrapy 2.1 来解析类别结果页面。
我想从该网站上抓取 2 种不同的东西:
- 类别信息,例如标题和网址
- 该类别页面中的产品项目
第 2 项有效,但我正在努力解决如何实现类别信息的存储。我的第一次尝试是创建另一个 Item Class CatItem:
class CatItem(scrapy.Item):
title = scrapy.Field() # char -
url = scrapy.Field() # char -
level = scrapy.Field() # int -
class ProductItem(scrapy.Item):
title = scrapy.Field() # char -
让我们解析页面:
def parse_item(self, response):
# save category info
category = CatItem()
category['url'] = response.url
category['title'] = response.url
category['level'] = 1
yield category
# now let's parse all products within that category
for selector in response.xpath("//article//ul/div[@data-qa-id='result-list-entry']"):
product = ProductItem()
product['title'] = selector.xpath(".//a/h2/text()").extract_first()
yield product
我的管道:
class mysql_pipeline(object):
def __init__(self):
self.create_connection()
def create_connection(self):
settings = get_project_settings()
def process_item(self, item, spider):
self.store_db(item, spider)
return item
现在我不知道如何继续。 process_item 定义中只有一个“项目”。
如何将类别信息也传递给 store_db 方法?
【问题讨论】:
标签: scrapy