我正在尝试从多级菜单获取所有链接。
起始URL=['https://www.bbcgoodfood.com/recipes/category/ingredients']
import scrapy
from foodisgood.items import FoodisgoodItem
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.loader import ItemLoader
from scrapy.loader.processors import TakeFirst
class BbcSpider(CrawlSpider):
name = 'bbc'
allowed_domains = ['bbcgoodfood.com']
start_urls = ['https://www.bbcgoodfood.com/recipes/category/ingredients']
rules = (
Rule(LinkExtractor(allow=(r'/recipes/category/[\w-]+$'), restrict_xpaths='//article[contains(@class, "cleargridindent")]'), callback='parse_sub_categories', follow=True),
Rule(LinkExtractor(allow=(r'/recipes/collection/[\w-]+$'), restrict_xpaths='//article[contains(@class, "cleargridindent")]'), callback='parse_collections', follow=True),
)
def parse_sub_categories(self, response):
l = ItemLoader(item=FoodisgoodItem(), response=response)
l.default_output_processor = TakeFirst()
l.add_xpath('category_title', '//h1[@class="section-head--title"]/text()')
l.add_value('page_url', response.url)
yield l.load_item()
def parse_collections(self, response):
l = ItemLoader(item=FoodisgoodItem(), response=response)
l.default_output_processor = TakeFirst()
l.add_xpath('collection_title', '//h1[@class="section-head--title"]/text()')
l.add_value('page_url', response.url)
yield l.load_item()
Results of menu scraping 但我不明白如何在集合标题前填充空的第一列。你知道吗
目前我有:
空的|牛排食谱| https://www.bbcgoodfood.com/recipes/collection/steak
但我需要:
肉|牛排食谱| https://www.bbcgoodfood.com/recipes/collection/steak
有人能告诉我需要做什么才能得到第一列子类别的结果吗?你知道吗
感谢所有人)
使用
CrawlSpider
的规则,您想要的并不是真正可行的(至少不是以一种简单的方式)。你知道吗通常的方法见Passing additional data to callback functions。
您将在第一次回调中提取类别,然后在
meta
dict中创建一个传递此信息的新请求相关问题 更多 >
编程相关推荐