在scrapy:Python中获取重定向的url时出错

2024-09-29 19:22:26 发布

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

我在使用代码:

def parse_find(self, response):
    Download_URL = "https://download.example.com/b/zMTY"
    request = Request(Download_URL, callback=self.parse_final)

    yield request

def parse_final(self, response):
    redirected_URL = response.url

    FileName = response.headers['Content-Disposition']  

    yield{   "Download_URL":redirected_URL,
             "FileName":FileName}

获取重定向的url及其标头,但scrapy提供了调试:

2018-07-30 04:41:49 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (
302) to <GET https://example.com/url> from <GET https://download.example.com/b/zMTY>
2018-07-30 04:41:51 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (
meta refresh) to <GET https://example.com> from <GET https://example.com/url>

必须注意的是,当我在scrapyshell中使用fetch提取这个url时,它会获取重定向的url并响应.标题很好用。你知道吗

我使用的是python3.6.5和scrapy1.5


Tags: httpsselfcomurlgetparseexampleresponse
2条回答

由于我无法使用scrapy,我已通过以下请求完成:

    Down = requests.get(response.url,allow_redirects=False)
    if Down.status_code == 302:
        redirected_URL = Down.headers['location']

这不是一个错误-这是一个debug日志。Scrapy通知您重定向是为了调试目的而发生的。您可以使用LOG_LEVEL设置更改残片日志级别。在dev环境之外,应该将其设置为LOG_LEVEL = 'INFO'

相关问题 更多 >

    热门问题