1. 程式人生 > >爬蟲302重定向問題

爬蟲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("**************")