王太歌 2022-01-20 23:02 采纳率: 66.7%
浏览 66
已结题

scrapy中start_requests指定回调函数为何没有调用

###### 问题遇到的现象和发生背景
ef start_requests(self):
        login_url = 'https://antispider7.scrape.center/api/login'
        header = {
            'Content-Type': 'application/json;charset=UTF-8',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
        }
        param = {
            'password': 'admin',
            'username': 'admin',
        }
        yield scrapy.Request(url=login_url, headers=header, body=json.dumps(param), method='POST', callback=self.login_callback)

    def login_callback(self, response):
        print(response.text)
        jwt = 'jwt ' + json.loads(response.text)['token']
        header = {
            'Authorization': jwt
        }
        for page in range(1, 3):
            url = 'https://antispider7.scrape.center/api/book/?limit=18&offset=%s' % ((page-1) * 18)
            yield scrapy.Request(url=url, headers=header, callback=self.parse_list)

    def parse_list(self, response):
        print(response.text)
        results = json.loads(response.text)
        for result in results['results']:
            book_id = result['id']
            title = result['name']
            score = result['score']
            author = ''.join(result['author']).replace('\n', '').replace('\t', '').replace('\r', '')
            outurl = result['cover']
            print(book_id, title, score, author, outurl)
问题相关代码,请勿粘贴截图

img

我需要先登录获取token加入到header中,因此我选择使用startr_requests来完成,但是yield之后始终没有任何回调函数捕捉到,请问可能的原因是什么?

  • 写回答

2条回答 默认 最新

  • 关注

    改成这样试试

    
            yield scrapy.Request(url=login_url, headers=header, body=json.dumps(param), method='POST', callback=lambda x,s=self: s.login_callback(x))
    
    

    你题目的解答代码如下:

        def start_requests(self):
            login_url = 'https://antispider7.scrape.center/api/login'
            header = {
                'Content-Type': 'application/json;charset=UTF-8',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
            }
            param = {
                'password': 'admin',
                'username': 'admin',
            }
            yield scrapy.Request(url=login_url, headers=header, body=json.dumps(param), method='POST', callback=lambda x,s=self: s.login_callback(x))
     
        def login_callback(self, response):
            print(response.text)
            jwt = 'jwt ' + json.loads(response.text)['token']
            header = {
                'Authorization': jwt
            }
            for page in range(1, 3):
                url = 'https://antispider7.scrape.center/api/book/?limit=18&offset=%s' % ((page-1) * 18)
                yield scrapy.Request(url=url, headers=header, callback=lambda x,s=self: s.parse_list(x))
     
        def parse_list(self, response):
            print(response.text)
            results = json.loads(response.text)
            for result in results['results']:
                book_id = result['id']
                title = result['name']
                score = result['score']
                author = ''.join(result['author']).replace('\n', '').replace('\t', '').replace('\r', '')
                outurl = result['cover']
                print(book_id, title, score, author, outurl)
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 已采纳回答 3月16日
  • 创建了问题 1月20日

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊