如何在scrapy中登录后重定向

2024-06-26 13:37:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个程序在scrapy登录在scrape相同的数据从一个网站。它似乎是正确登录,据我所知,但当它登录它重定向到一个网页,我不希望它刮。我需要它去这个网页,https://www.starcitygames.com/myaccount/。如何让我的程序在登录后转到该页面

import scrapy
from ..items import NameItem

class LoginSpider(scrapy.Spider):
    name = "LoginSpider"
    start_urls = ["https://www.starcitygames.com/login"]

    def parse(self, response):

        return scrapy.FormRequest.from_response(
        response,
        formdata={'ex_usr_email': 'username', 'ex_usr_pass': 'password'},
        callback=self.after_login
     )

    def after_login(self, response):
        item = NameItem()
        item["Name"] = response.css("div.column_data::text").get()
        return item

Tags: fromhttpsimportself程序com网页response
1条回答
网友
1楼 · 发布于 2024-06-26 13:37:06

您需要分为两个步骤:

def after_login(self, response):
    yield scrapy.Request(url="https://www.starcitygames.com/myaccount/", callback=self.parse_data)

def parse_data(self, response):
    item = NameItem()
    item["Name"] = response.css("div.column_data::text").get()
    return item

相关问题 更多 >