302重定向301后,如何获取第一个请求网址

xie

我用scrapy(ver:1.1.1)在互联网上刮了一些日期。这就是我要面对的:

class Link_Spider(scrapy.Spider):
    name = 'GetLink'
    allowed_domains = ['example_0.com']
    with codecs.open('link.txt', 'r', 'utf-8') as f:
        start_urls = [url.strip() for url in f.readlines()]

def parse(self, response):
    print response.url

在上面的代码中,“ start_urls”类型是一个列表:

start_urls = [
              example_0.com/?id=0,
              example_0.com/?id=1,
              example_0.com/?id=2,
             ] # and so on

当草率运行时,调试信息告诉我:

[scrapy] DEBUG: Redirecting (302) to (GET https://example_1.com/?subid=poison_apple) from (GET http://example_0.com/?id=0)
[scrapy] DEBUG: Redirecting (301) to (GET https://example_1/ture_a.html) from (GET https://example_1.com/?subid=poison_apple)
[scrapy] DEBUG: Crawled (200) (GET https://example_1/ture_a.html) (referer: None)

现在,如何知道“ start_url”中“ http://example_0.com/?id= ***”的哪个URL与“ https://example_1/ture_a.html的URL成对有人可以帮助我吗?

LR

扩展答案,如果您希望控制每个请求而无需自动重定向(因为重定向是一个额外的请求),则可以禁用RedirectMiddleware或仅将meta参数传递dont_redirect给该请求,因此在这种情况下:

class Link_Spider(scrapy.Spider):
    name = 'GetLink'
    allowed_domains = ['example_0.com']

    # you'll have to control the initial requests with `start_requests`
    # instead of declaring start_urls

    def start_requests(self):
        with codecs.open('link.txt', 'r', 'utf-8') as f:
            start_urls = [url.strip() for url in f.readlines()]
        for start_url in start_urls:
            yield Request(
                start_url, 
                callback=self.parse_handle1, 
                meta={'dont_redirect':True, 'handle_httpstatus_list': [301, 302]},
            )
    def parse_handle1(self, response):
        # here you'll have to handle the redirect yourself
        # remember that the redirected url is in in the header: `Location`
        # do something with the response.body, response.headers. etc.
        ...
        yield Request(
            response.headers['Location'][0], 
            callback=self.parse_handle2,
            meta={'dont_redirect':True, 'handle_httpstatus_list': [301, 302]},
        )

    def parse_handle2(self, response):
        # here you'll have to handle the second redirect yourself
        # do something with the response.body, response.headers. etc.
        ...
        yield Request(response.headers['Location'][0], callback=self.parse)


    def parse(self, response):
        # actual last response
        print response.url

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何301重定向-将我网站的所有网址都重定向到一个页面

如何从网址获取变量并使用php 301重定向?

如何做“两个301重定向到一个301重定向” Wordpress

Python请求:如何从302重定向获取响应cookie

301仅重定向第一个子文件夹

OPLON LBL ADC重写302重定向位置URL,它们在301中以“ /”结尾,没有最后一个“ /”

搜索仅包含一个结果后的301重定向用户是否合适?

我有一个指向www的302重定向。但Googlebot不断抓取非www网址

使用RewritePath不能在第一个请求时从Application-BeginRequest重定向

如何从图像网址数组中获取第一个jpg / png图像网址?

更改网址结构后,nginx 301重定向

更改网址中的单词后301重定向

如何保留第一个HTTP(重定向)响应?

如何使用相同的网址结构进行301重定向?

为什么从 nodejs 服务器重定向仅适用于第一个请求?

是会话的第一个请求后自动创建?

301重定向到所有获取请求

如何将一个网址重定向到另一个网址?

如何在成功的ajax post请求后发送一个值到页面重定向

AJAX请求完成后如何重定向到另一个页面

301重定向目标网址格式

单击按钮后获取第一个div

从网址获取第一个和最后一个文件夹

如何在Smarty中截断请求URI以仅获取第一个块?

如何仅获取此请求 Json 中括号之间的第一个值

将具有多个参数的301网址重定向到另一个网址

我如何重定向到第一个子组件vue路由器

如何根据写入输入字段(PHP)的第一个字母重定向到页面

301重定向不起作用(我知道另一个)