爬虫302重定向问题

可以通过判断请求状态分别进行不同的程序、

def down_load(url):
    headers1 = {
    *******************
}
    html = requests.get(url=url,headers=headers1,allow_redirects=False)
    print(html.status_code)
    # print(html.headers["location"])
    if html.status_code == 302:
        new_id_url="http://www.***.com"+html.headers["location"]
        print(new_id_url)
        new_html=requests.get(url=new_id_url,headers=headers1).text
        return etree.HTML(new_html)
    else:
        print("++++++++++++++++")
        print(url)
        # print(requests.get(url=url,headers=headers1).text)
        return etree.HTML(requests.get(url=url,headers=headers1).text)
down_load("**************")

猜你喜欢

转载自blog.csdn.net/weixin_42357472/article/details/85263776