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

来源:3-2 使用urllib发送post请求

iamYvette

2017-06-11 16:30

代码:

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'


写回答 关注

1回答

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

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

    是不是没添加python的path?

    iamYve...

    我之前找到了原因。脚本对应文件夹下有我用jupyter notebook创建的.ipynb就发生了这个问题。我一删除该ipynb文件或者将其移至其他文件夹就没问题了。但我还不太明白造成这个问题的原因是什么。因为使用pycharm创建的jupyter就没问题。。。通过终端创建的就发生问题

    2017-06-28 21:16:12

    共 1 条回复 >

python遇见数据采集

本教程让你初步掌握Python进行数据采集,创造属于你的价值

59669 学习 · 200 问题

查看课程

相似问题