所以当我从终端运行它时,我的python3脚本工作,但是当我从php脚本调用它时(仅使用exec,我在终端命令中输入相同的东西)。
脚本确实运行,但某些事情会导致错误(我不知道如何获取错误消息,只有退出代码 - 0或1)。首先,字符串连接(使用string1 + string2)不起作用,所以我不得不使用%占位符。
我目前要解决的问题是网址请求。我尝试过使用
requests.get(url)
和
req = urllib.request.Request(url)
with urllib.request.urlopen(req)
它们都可以从终端工作,并且PHP脚本在被注释掉时会起作用/不会出错。有谁知道为什么会这样,以及如何解决它?
修改
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "./scripts/getSBNews.py", line 49, in getSBNews
response = urllib.request.urlopen(req)
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request