I want to use scrapy to crawl a json.Here is my code:
class zhangjiaweiSpider(scrapy.Spider):
name = "zhangjiawei"
start_urls = [
"https://zhuanlan.zhihu.com/api/columns/zhangjiawei/posts?limit=20&offset="
]
def start_requests(self):
for i in range(1):
url = self.start_urls[0] + str(i * 20)
yield scrapy.Request(url,callback = self.parse)
def parse(self, response):
jsonbody = json.loads(response.body.decode('utf-8','ignore'))
print(jsonbody)
But when I run it,I get errors:
Traceback (most recent call last):
File "d:\soft\python\lib\site-packages\twisted\internet\defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "D:\code\python\spider\zhihuzhuanlan\zhihuzhuanlan\spiders\zhangjiaweispider.py", line 24, in parse
jsonbody = json.loads(response.body.decode('utf-8','ignore'))
File "d:\soft\python\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "d:\soft\python\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "d:\soft\python\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I tried to print response.body.decode(), but got error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 0:
invalid start byte
if I print response.body.decode('utf-8','ignore') they are Garbled.
I thought this error may be caused by response.body's decode. But I don't know how to resolve this problem.
my setting.py:
BOT_NAME = 'zhihuzhuanlan'
SPIDER_MODULES = ['zhihuzhuanlan.spiders']
NEWSPIDER_MODULE = 'zhihuzhuanlan.spiders'
DEFAULT_REQUEST_HEADERS = {
'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding':'gzip, deflate, br',
'accept-language':'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6',
'USER-AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
}
ITEM_PIPELINES = {
'zhihuzhuanlan.pipelines.ArticleDataBasePipeline': 5,
}
FEED_EXPORT_ENCODING = 'utf-8'
# linux pip install MySQL-python
DATABASE = {'drivername': 'mysql',
'host': '192.168.203.95',
'port': '3306',
'username': 'root',
'password': 'Password',
'database': 'spider',
'query': {'charset': 'utf8'}}
ROBOTSTXT_OBEY = True
Related
I have billing enabled for my project, however, I keep on getting an error returned:
Error: could not handle the request
And the traceback:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/requests/models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "/opt/python3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/opt/python3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/python3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Here is my code:
import json
from google.cloud import storage
import requests
def call(request):
url = "https://jsonplaceholder.typicode.com/todos/"
response = requests.get(url)
json_data = response.json()
pretty_json = json.dumps(json_data)
print(pretty_json)
With requirements being:
requests
google.cloud.storage
The following should work for you:
Create a virtualenv
python3 -m venv venv
source venv/bin/activate
Install Requests (you're not using Google Cloud Storage)
python3 -m pip install requests
Script
import json
import requests
url = "https://jsonplaceholder.typicode.com/todos/"
response = requests.get(url)
print(response.status_code)
print(response.text)
Execute the code
python3 main.py
Results
200
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
...
]
I have error in the following code when I try to send a request with json data in it, is it because it only works synchronously, so does not work in fastapi which work asynchronously? If that is the case, what is the simplest way to send a request with json data in it?
import time
from fastapi import Request, FastAPI, BackgroundTasks
import multiprocessing as mp
import uvicorn
import requests
import json
def printmessage(job):
time.sleep(5)
print(job)
if __name__ == 'webhook_fastapi':
url = 'http://127.0.0.1:8000/webhook'
data = { 'text': 'hello'}
r = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
print("Request Sent!")
app = FastAPI()
#app.post("/webhook")
async def webhook(request : Request, background_tasks: BackgroundTasks):
print("WEBHOOK RECEIVED")
job="doctor"
background_tasks.add_task(printmessage,job)
print('done')
return 'WEBHOOK RECEIVED'
if __name__ == '__main__':
print("PROGRAM LAUNCH...")
print("WEBHOOK RECEIVE READY...")
background_tasks = BackgroundTasks()
job="doctor"
background_tasks.add_task(printmessage,job)
uvicorn.run("webhook_fastapi:app", reload=False)
OUTPUT:
__main__
PROGRAM LAUNCH...
WEBHOOK RECEIVE READY...
webhook_fastapi
Traceback (most recent call last): File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 169, in _new_conn
conn = connection.create_connection( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 96, in create_connection
raise err File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 86, in create_connection
sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused
it
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 699, in urlopen
httplib_response = self._make_request( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 394, in _make_request
conn.request(method, url, **httplib_request_kw) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1279, in request
self._send_request(method, url, body, headers, encode_chunked) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1325, in _send_request
self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1274, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1034, in _send_output
self.send(msg) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 974, in send
self.connect() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 200, in connect
conn = self._new_conn() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 181, in _new_conn
raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000013C0E494310>:
Failed to establish a new connection: [WinError 10061] No connection
could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py",
line 439, in send
resp = conn.urlopen( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 755, in urlopen
retries = retries.increment( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py",
line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1',
port=8000): Max retries exceeded with url: /webhook (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x0000013C0E494310>: Failed to establish a new connection: [WinError
10061] No connection could be made because the target machine actively
refused it'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"c:\Users*\webhook_fastapi.py", line 40, in
uvicorn.run("webhook_fastapi:app", reload=False) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\main.py",
line 463, in run
server.run() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\server.py",
line 60, in run
return asyncio.run(self.serve(sockets=sockets)) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py",
line 44, in run
return loop.run_until_complete(main) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py",
line 642, in run_until_complete
return future.result() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\server.py",
line 67, in serve
config.load() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\config.py",
line 458, in load
self.loaded_app = import_from_string(self.app) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\importer.py",
line 21, in import_from_string
module = importlib.import_module(module_str) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib_init_.py",
line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File
"", line 1007, in _find_and_load File
"", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in
exec_module File "", line 228, in
_call_with_frames_removed File "c:\Users*\webhook_fastapi.py", line 19, in
r = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'}) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py",
line 117, in post
return request('post', url, data=data, json=json, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py",
line 61, in request
return session.request(method=method, url=url, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py",
line 542, in request
resp = self.send(prep, **send_kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py",
line 655, in send
r = adapter.send(request, kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py",
line 516, in send
raise ConnectionError(e, request=request) requests.exceptions.ConnectionError:
HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded
with url: /webhook (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x0000013C0E494310>: Failed to establish a new connection: [WinError
10061] No connection could be made because the target machine actively
refused it')) PS C:\Users*>
There is no server running when you're trying to make your request. uvicorn is importing your code, and you're apparently trying to make a request while the code is being imported.
uvicorn can't launch the API and bind to the port before it has finished importing the code and the ASGI application has been set up and properly configured.
I want know how open correctly new connection to database. A problem rise when i want create single QUERY to database, from many places in my code. For example, one request is sent to database from login.html , second from register page, third to gallery in index.html . I guess , should i use some project patterns? Do you have some suggestion ?
I have written this class which are responsible for connection to mysql database. I suppose that it is very wrong (i don't focus yet on validation - but i know about that):
import mysql.connector
import configparser
class DataBaseConnectionHandler(object):
__DATABASE = ""
user = ""
passwd = ""
host = ""
fileName = ""
dataBaseConnection = ""
def __init__(self, fileName=None, host='localhost'):
if fileName is not None:
config_file = configparser.ConfigParser()
config_file.read('shop/config/dbConfig.ini')
database: str = config_file.get('DEFAULT', 'database')
user: str = config_file.get('DEFAULT', 'user')
password: str = config_file.get('DEFAULT', 'password')
self.__DATABASE = database
self.user = user
self.passwd = password
self.host = host
def connectToDatabase(self):
""" EXECUTE CONNECTION AND RETURN AN HOOK TO DATABASE"""
dataBaseConnector = mysql.connector.connect(
host=self.host,
user=self.user,
passwd=self.passwd,
database=self.__DATABASE
)
if dataBaseConnector != "":
self.dataBaseConnection = dataBaseConnector
return self.dataBaseConnection
else:
self.dataBaseConnection = None
return self.dataBaseConnection
class RegisterUser
from .databses import DataBaseConnection
class RegisterUser(object):
__formName = ""
__formLogin = ""
__formSurrname = ""
__formPasswd = ""
__fromEmail = ""
def __init__(self, userName, userSurrname, userLogin, userPassword, userEmail):
self.__formName = userName
self.__formSurrname = userSurrname
self.__formLogin = userLogin
self.__formPasswd = userPassword
self.__fromEmail = userEmail
def createUser(self):
print("[!] Create user")
# ------------- CONNECTION TO DATABASE ----------------
hook = DataBaseConnection.DataBaseConnectionHandler('shop/config/dbConfig.ini').connectToDatabase()
myCursor = hook.cursor()
# ------------- EXECUTING MYSQL QUERY ----------------
# [!] nie szyfruje hasła
sqlStatment = "INSERT INTO user(Login, Password, Email, Name, Surname) " \
"VALUES ('{}', '{}', '{}', '{}', '{}');".format(self.__formLogin,
self.__formPasswd,
self.__fromEmail,
self.__formName,
self.__formSurrname)
myCursor.execute(sqlStatment)
hook.commit()
print(myCursor.rowcount, "Record inserted")
print("[*] Executing an query")
# ------------- CLOSE CONNECTION ----------------
hook.close()
I know that Django has own way to connect to database, but it is wired for me. i had some wired error, that why i decide use PyMysql. In near time I'll change that, but it's good moment to learn how to write code correctly .
edit:
PycharmProjects/lovLevelMusic/lovLevelMusic/settings.py changed, reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python3.7/dist-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/usr/local/lib/python3.7/dist-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.7/dist-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/usr/local/lib/python3.7/dist-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/local/lib/python3.7/dist-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/usr/local/lib/python3.7/dist-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/options.py", line 204, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/usr/local/lib/python3.7/dist-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python3.7/dist-packages/django/db/utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/local/lib/python3.7/dist-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/mysql/base.py", line 22, in <module>
from MySQLdb.constants import CLIENT, FIELD_TYPE # isort:skip
ImportError: cannot import name 'CLIENT' from 'MySQLdb.constants' (unknown location)
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbDjango',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
i'm using python tornado to build a simple web server. Here is the code of tornado:
import json
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=80, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self, param):
print("\n\nthis is a get request from indexhandler:")
if param:
print("param is NOT null")
self.render(r"frontend/" + param)
else:
print("param is null")
self.render(r"frontend/index.html")
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/(.*)", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
All of the frontend codes are in the directory /frontend so I used a simple regex (.*) to permit user to access all of resources in /frontend, such as js files and css files.
However, when I try to visit my website, I get some 304 errors at the server:
[I 170501 14:31:59 web:2063] 200 GET /html/country.html
[I 170501 14:31:59 web:2063] 304 GET /css/bootstrap.min.css
[I 170501 14:31:59 web:2063] 304 GET /css/reset.css
[I 170501 14:31:59 web:2063] 304 GET /css/icon/iconfont.css
[I 170501 14:31:59 web:2063] 304 GET /css/country.css
[I 170501 14:31:59 web:2063] 304 GET /css/common.css
UPDATE
I have another issue: error 500
In a word, I have some 304 and some 500. All of 500 are like this:
[E 170501 22:53:19 web:1590] Uncaught exception GET /images/main-img1.jpg (X.X.X.X)
HTTPServerRequest(protocol='http', host='X.X.X.X', method='GET', uri='/images/main-img1.jpg', version='HTTP/1.1', remote_ip='X.X.X.X', headers={'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', 'Accept': 'image/webp,image/*,*/*;q=0.8', 'Host': 'X.X.X.X', 'Referer': 'http://X.X.X.X/html/country.html', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'})
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 1509, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "tmp.py", line 20, in get
self.render("frontend/" + param)
File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 724, in render
html = self.render_string(template_name, **kwargs)
File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 862, in render_string
t = loader.load(template_name)
File "/usr/local/lib/python3.5/site-packages/tornado/template.py", line 427, in load
self.templates[name] = self._create_template(name)
File "/usr/local/lib/python3.5/site-packages/tornado/template.py", line 455, in _create_template
template = Template(f.read(), name=name, loader=self)
File "/usr/local/lib/python3.5/site-packages/tornado/template.py", line 304, in __init__
reader = _TemplateReader(name, escape.native_str(template_string),
File "/usr/local/lib/python3.5/site-packages/tornado/escape.py", line 218, in to_unicode
return value.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
[E 170501 22:53:19 web:2063] 500 GET /images/main-img1.jpg (X.X.X.X) 2.07ms
HTTP 304 is "Not Modified". It's not an error, it's an optimization. Tornado (like most web servers) tells your browser each static file's last-modified-date and a checksum of its contents (its "ETag" in HTTP terms). When the browser requests the file again, the browser tells Tornado which last-modified date and ETag it has in the browser's cached copy; Tornado compares those to its own and, if they haven't changed, just tells the browser "304 Not Modified". Thus, the browser knows it can use its cached copy and doesn't have to re-download the original.
The HTTP 500 is the actual problem. You have some characters in your template file that are not valid UTF-8. Apparently the very first character is not valid UTF-8, based on the "position 0" in the error message.
I'm trying to navigate between 2 HTML pages through Tornado. Following is the code for the routes and their respective handlers:
class MainHandler(tornado.web.RequestHandler):
def get(self):
log.info("Rendering index.html")
self.render("index.html")
class NotificationsPageHandler(tornado.web.RequestHandler):
def get(self):
log.info("Rendering notifications")
self.render("notifications.html")
def start_server():
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static")
}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/notifications.html", NotificationsPageHandler),
], **settings)
application.listen(8989)
tornado.ioloop.IOLoop.current().start()
When I load 127.0.0.1:8989 on the browser, I get the index.html page but when I try to navigate to notifications.html through an anchor tag in index.html, I get the following stack trace:
2016-07-06 12:07:06,546 - tornado.application - ERROR - Uncaught exception GET /notifications.html (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8989', method='GET', uri='/notifications.html', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Host': '127.0.0.1:8989', 'Upgrade-Insecure-Requests': '1', 'Accept-Encoding': 'gzip, deflate, sdch', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36', 'Referer': 'http://127.0.0.1:8989/', 'Connection': 'keep-alive'})
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 1443, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "BADWebServer.py", line 231, in get
self.render("notifications.html")
File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 699, in render
html = self.render_string(template_name, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 806, in render_string
return t.generate(**namespace)
File "/usr/local/lib/python3.5/dist-packages/tornado/template.py", line 345, in generate
return execute()
File "notifications_html.generated.py", line 5, in _tt_execute
_tt_tmp = item.score # notifications.html:37
NameError: name 'item' is not defined
2016-07-06 12:07:06,548 - tornado.access - ERROR - 500 GET /notifications.html (127.0.0.1) 4.51ms
I have seen a similar post, how to navigate from one html to other in tornado using anchor tag but I'm not sure why I'm getting the exception.
You're getting the error because, as the trace says, "name 'item' is not defined". Your notifications.html template contains some markup like:
{{ item.score }}
... but you haven't passed an "item" variable in. See the template syntax guide for an example.