r.history-我想获取它重定向到的链接

时间:2019-06-18 13:29:12

标签: python python-requests

我有一些代码,可以通过API获取《纽约时报》文章的元数据。有些链接是redirects \ timed链接,我需要指向article的真正链接。该链接不应该转到sqlite数据库。这是我尝试过的:

查看代码

if r.history:
            print ("Request was redirected")
            for resp in r.history:
                print (resp.status_code, resp.url)
                funktioniert nicht --> bricht per fehler ab
                redirect = str(resp.url)
                Redirect_url_exists = False
                try:
                    if database.execute('SELECT COUNT(*) FROM nyt WHERE redirect = ?', (redirect,)).fetchone()[0] > 0:
                        Redirect_url_exists = True
                        break

                except:
                    pass
            if Redirect_url_exists:
                continue    

            print ("Final destination:")
            print (response.status_code, response.url)
        else:
            print ("Request was not redirected")
         print(r.headers)

没有写下错误...但是当我全部跳过时,它又可以工作了。

0 个答案:

没有答案