首页 > 解决方案 > HTTP 错误 307:Python3 中的临时重定向 - INTRANET

问题描述

该代码生成一系列 URL 并在其中搜索特定字符串。由于网站需要登录信息:

这是代码:

         url ='https://www.aug.ipp.mpg.de/cgibin/sfread_only/isis?'

    shotn = shot_a
    #Shot_a, shot_z, diag and param are user inputs.
    enter code here
    while (shotn <= shot_z):
        values = {'shot': shotn,
                  'exp': 'AUGD',
                  'diag': diag ,
                  'action': 'SignalDetails',
                  'signal': param}



       data = urllib.parse.urlencode(values)
        data = data.encode('utf-8')
        req = urllib.request.Request(url, data)
        resp = urllib.request.urlopen(req)
       #The upper line is line 42- the first error
        respData = resp.read()
       shotn +=1

预期的结果将是.txt计算机中的一个文件,其中包含shotn's在相应 URL 中找到的特定语句。


这是实际结果:

Traceback (most recent call last):
  File "C:/Users/lenovo/PycharmProjects/ url/venv/Final.py", line 42, in <module>
    resp = urllib.request.urlopen(req)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 734, in http_error_302
    new = self.redirect_request(req, fp, code, msg, headers, newurl)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 672, in redirect_request
    raise HTTPError(req.full_url, code, msg, headers, fp)
urllib.error.HTTPError: HTTP Error 307: Temporary Redirect

Process finished with exit code 1

标签: pythonpython-3.xerror-handlingurllib

解决方案


推荐阅读