Я пытался просканировать заголовки рецептов из пищевой сети и хочу рекурсивно перейти на следующую страницу. Я использую python 3, поэтому некоторые функции в scrapy мне недоступны, но вот что у меня есть:
import scrapy
from scrapy.http import Request
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import Selector
from scrapy.selector import HtmlXPathSelector
from testspider.items import testspiderItem
from lxml import html
class MySpider(CrawlSpider):
name = "test"
allowed_domains = ["foodnetwork.com"]
start_urls = ["http://www.foodnetwork.com/recipes/aarti-sequeira/middle-eastern-fire-roasted-eggplant-dip-babaganoush-recipe.html"]
rules = (Rule(LinkExtractor(allow=(), restrict_xpaths=('//div[@class="recipe-next"]/a/@href',)), callback="parse_page", follow= True),)
def parse(self, response):
site = html.fromstring(response.body_as_unicode())
titles = site.xpath('//h1[@itemprop="name"]/text()')
for title in titles:
item = testspiderItem()
item["title"] = title
yield item
Теги из источника веб-страницы:
<div class="recipe-next">
<a href="/recipes/food-network-kitchens/middle-eastern-eggplant-rounds-recipe.html">Next Recipe</a>
</div>
Любая помощь будет оценена по достоинству!