问答详情
源自:3-2 使用urllib发送post请求

按照老师的代码敲的,为什么会报错?求大神解答

代码:

from urllib.request import urlopen
from urllib.request import Request
from urllib import parse
req = Request("http://www.thsrc.com.tw/tw/TimeTable/SearchResult")
postData = parse.urlencode([
    ("StartStatio","2f940836-cedc-41ef-8e28-c2336ac8fe68"),
    ("EndStation","977abb69-413a-4ccf-a109-0272c24fd490"),
    ("SearchDate","2017/06/11"),
    ("SearchTime","13:00"),
    ("SearchWay","DepartureInMandarin")
])
req.add_header("Origin","http://www.thsrc.com.tw")
req.add_header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
resp = urlopen(req, data=postData.encode('utf-8'))
print(resp.read().decode("utf-8"))

错误:

Fatal Python error: Py_Initialize: can't initialize sys standard streams

Traceback (most recent call last):

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/io.py", line 52, in <module>

  File "/Users/Yvette/PycharmProjects/2017/201705/abc.py", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 84, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 9, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 122, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/enum.py", line 2, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/types.py", line 171, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/functools.py", line 20, in <module>

ImportError: cannot import name 'get_cache_token'


提问者:iamYvette 2017-06-11 16:30

个回答

  • mk充满爱与正义
    2017-06-14 15:24:11

    代码我电脑python3.6能运行成功

    是不是没添加python的path?