python - HTTP 错误 307:Python3 中的临时重定向 - INTRANET
问题描述
该代码生成一系列 URL 并在其中搜索特定字符串。由于网站需要登录信息:
我通过浏览器登录了该网站。
为了进行更多检查,我只尝试了一个完整的 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
解决方案
推荐阅读
- tensorflow - 在 TensorBoard 中显示分布和激活的更优雅的方式
- java - 如何在没有嵌套if的情况下执行多个if
- c# - IBackgroundTask 与 IDisposables
- bash - MacOS Bash find 和 echo 没有按预期工作?
- java - Servlet Exception( java.lang.StackOverflowError ) 用于将值从表插入到另一个
- firebase - firebase web app和firebase托管有什么区别
- reactjs - 将鼠标悬停在选项上时反应选择,打开子菜单
- java - getCanonicalHostName 正在返回不合格的名称
- reactjs - npm install webpack config.js
- c++ - 前向声明如何隐藏依赖项并导致用户代码跳过必要的重新编译?(谷歌风格指南 C++)