1、NotImplementedError
import tornado.ioloop
import tornado.web
import tornado.httpserver
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, Nowamagic")
application = tornado.web.Application([(r"/", MainHandler)])
# httpServer = tornado.httpserver.HTTPServer(application)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
解决方法:
https://ask.csdn.net/questions/1054691
找到tornado/platform/asyncio.py 文件
在文件里面加上
import sys
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
2、json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
import json
import tornado.httpclient
http_client = tornado.httpclient.HTTPClient()
try:
response = http_client.fetch("https://news-at.zhihu.com/api/4/news/latest")
# print(response.body)
except httpclient.HTTPError as e:
print("Error:", e)
http_client.close()
调用url出来的数据是bytes类型,想变成json类型,先转换成str,然后再转换成json发现有报错
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
经过查找发现是,没有转换成 utf-8 编码
data_str = str(data.decode(encoding='utf-8'))
转换后即可
3. urllib2.Request(url,head,data) 内部参数
head :dict类型
data :str类型,同时,发起请求所含的必要参数应当在当中。如同postman中的body一样。
4. urlopen()获取页面,返回类型为bytes,需要用decode解码,转换成str类型