首页 > 解决方案 > 使用 **requests.get** 和 python 来处理 HTTP 重定向?

问题描述

我在 python 上的请求模块有问题,当用户在我的基本网络抓取应用程序的网页上输入 url 时,请求模块不处理重定向 [例如:http ://www.yahoo.com - -> ie.yahoo.com] 破坏应用程序。按照代码片段,谢谢

    if request.method == 'POST' and form.validate():
    # the following are the data from the init form
        url = form.url.data
        email = form.email.data

        # defining a new variable taking as input the values from the init form
        mymsg=[{
            "url": url, 
            "email" : email, 
              }]
    # insert the list into the mongo db
        x = mycol.insert_many(mymsg), print("inserting this item: ", mymsg, "in the database called ", mycol)

        url1="http://"+url #making sure the parser will get the HTTP://WWW.EXAMPLE.COM syntax
        result = requests.get(url1)
        print("the requested url is: ",url1)
        print(result.status_code)            
        print(result.headers)

标签: python

解决方案


推荐阅读