从Scrapy请求获取重定向页面

时间:2013-10-13 05:32:25

标签: python html url redirect scrapy

我正在使用Scrapy的FormRequest类登录网站并删除其数据。但是,我遇到的问题是获得生成的登录页面。在我提交表单请求后,我被重定向到重定向页面,但我想要的页面是重定向页面之后的页面。在常规浏览器中,重定向页面会自动重定向到主页,但在Scrapy蜘蛛中,重定向页面是响应并被困在那里。

如何通过此重定向页面以获取主页?

以下代码:

from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request

class DmozSpider(BaseSpider):
    name = "dmoz"
    start_urls = [
                "https://blahblahblahsecurepage"
            ]

    def parse_evalPage(self, response):
        filename = response.url.split("?")[-1].split("&")[0]
        open(filename, "wb").write(response.body)

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        if hxs.select("//form[@id='loginform']"):
            return self.login(response)

    def after_login(self, response):
        # check login succeed before going on
        if "Please enter a valid username" in response.body:
            self.log("Login failed", level=log.ERROR)
            return
        # We've successfully authenticated, let's have some fun!
        else:
            return Request(response, callback=self.parse_evalPage)

    def login(self, response):
        return [FormRequest.from_response(response,
                    formdata={'j_username': 'XXXXXXXXXXXX', 'j_password': 'XXXXXXXXXXXXXX'},
                    formnumber = 1, callback=self.parse_evalPage)]

0 个答案:

没有答案