import scrapy
from bs4 import BeautifulSoup
 
class AdvancedSpider(scrapy.Spider):
    name = 'advanced'
    allowed_domains = ['example.com']
    start_urls = ['http://example.com/pages.html']
 
    def parse(self, response):
        # 使用Scrapy的Selector
        links = response.css('a::attr(href)').getall()
        for link in links:
            # 请求下一页,并在回调函数中处理响应
            yield response.follow(link, self.parse_page)
 
    def parse_page(self, response):
        # 使用BeautifulSoup解析页面
        soup = BeautifulSoup(response.text, 'html.parser')
        title = soup.find('h1').text
        content = soup.find('div', {'class': 'content'}).text
 
        # 存储数据
        yield {
            'title': title,
            'content': content,
        }这个例子展示了如何结合Scrapy的response.follow方法和BeautifulSoup进行更高级的数据抓取。在parse方法中,我们使用Scrapy的CSS选择器来获取页面中所有的链接,并通过response.follow方法递归地抓取每一个页面。在parse_page回调函数中,我们使用BeautifulSoup来提取页面中的标题和内容,并生成一个包含这些数据的字典。这个字典可以被Scrapy用来进一步处理或存储数据。