您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Python中遵循元刷新

如何在Python中遵循元刷新

好的,似乎没有库支持它,因此我一直在使用以下代码

import urllib2
import urlparse
import re

def get_hops(url):
    redirect_re = re.compile('<Meta[^>]*?url=(.*?)["\']', re.IGNORECASE)
    hops = []
    while url:
        if url in hops:
            url = None
        else:
            hops.insert(0, url)
            response = urllib2.urlopen(url)
            if response.geturl() != url:
                hops.insert(0, response.geturl())
            # check for redirect Meta tag
            match = redirect_re.search(response.read())
            if match:
                url = urlparse.urljoin(url, match.groups()[0].strip())
            else:
                url = None
    return hops
python 2022/1/1 18:26:34 有196人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶