Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 状态代码为200的链接重定向_Python_Python 3.x_Redirect_Python Requests - Fatal编程技术网

Python 状态代码为200的链接重定向

Python 状态代码为200的链接重定向,python,python-3.x,redirect,python-requests,Python,Python 3.x,Redirect,Python Requests,我有一个链接,其状态代码为200。但当我在浏览器中打开它时,它会重定向 在使用Python请求获取同一链接时,它只显示来自原始链接的数据。我尝试了Python请求和urllib,但没有成功 如何捕获最终URL及其数据 状态为200的链接如何重定向 >>url='1!'http://www.afaqs.com/news/story/52344_The-target-is-to-get-advertisers-to-switch-from-print-to-TV-Ravish-Kumar-Viaco

我有一个链接,其状态代码为200。但当我在浏览器中打开它时,它会重定向

在使用Python请求获取同一链接时,它只显示来自原始链接的数据。我尝试了Python请求和urllib,但没有成功

  • 如何捕获最终URL及其数据

  • 状态为200的链接如何重定向

  • >>url='1!'http://www.afaqs.com/news/story/52344_The-target-is-to-get-advertisers-to-switch-from-print-to-TV-Ravish-Kumar-Viacom18'
    >>>r=请求。获取(url)
    >>>r.url
    'http://www.afaqs.com/news/story/52344_The-target-is-to-get-advertisers-to-switch-from-print-to-TV-Ravish-Kumar-Viacom18'
    >>>r.历史
    []
    >>>r.状态代码
    200
    


    这种重定向是由JavaScript完成的。因此,您不会使用
    请求直接获取重定向链接。get(…)
    。原始URL具有以下页面源:

    <html>
        <head>
            <meta http-equiv="refresh" content="0;URL=http://www.afaqs.com/interviews/index.html?id=572_The-target-is-to-get-advertisers-to-switch-from-print-to-TV-Ravish-Kumar-Viacom18">
            <script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/D5838D60-3633-1046-AA3A-D5DDF145A207/main.js" charset="UTF-8"></script>
        </head>
        <body bgcolor="#FFFFFF"></body>
    </html>
    
    或者,使用正则表达式:

    redirected_url = re.findall(r'URL=(http.*)">', r.text)[0]
    

    这些类型的url存在于脚本标记中,因为它们是javascript代码。因此,python也不会获取它们

    要获得链接,只需从各自的标签中提取它们

    redirected_url = re.findall(r'URL=(http.*)">', r.text)[0]