advancerHF 2020-08-21 10:40 采纳率: 33.3%
浏览 816

TypeError: __init__() got an unexpected keyword argument 'callback'

谁能告诉我出了什么错?
代码如下:

from tornado.httpclient import AsyncHTTPClient
import os
from tornado.ioloop import IOLoop

# 异步客户端
def parse(content):
    import bs4
    bs = bs4.BeautifulSoup(content,'html.parser')
    h4list = [h4.text for h4 in bs.select('ul.foot_nav.main h4')]
    for h in h4list:
        print(h)

def handle_response(response):
    body = response.body


    file = os.path.join(os.getcwd(),'templates','index.html')

    with open(file,'wb') as fw:
        fw.write(body)

    parse(body)




def load1(url,callback):
    http_client = AsyncHTTPClient()
    http_client.fetch(url,callback=callback)


load1(url='http://www.bjsxt.com',callback=handle_response)


IOLoop.instance().start()

报以下错误:

Traceback (most recent call last):
  File "E:/软件下载/Python/untitled/2020.8.13/tornado/test2/test2.py", line 32, in <module>
    load1(url='http://www.bjsxt.com',callback=handle_response)
  File "E:/软件下载/Python/untitled/2020.8.13/tornado/test2/test2.py", line 29, in load1
    http_client.fetch(url,callback=callback)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\tornado\httpclient.py", line 284, in fetch
    request = HTTPRequest(url=request, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'callback'
  • 写回答

2条回答 默认 最新

  • jingluan666 2020-08-21 11:26
    关注

    检查下安装的tornado版本,fetch方法说明有这么一段,从6.0开始已经移除了callback

      .. versionchanged:: 6.0
    
               The ``callback`` argument was removed. Use the returned
               `.Future` instead.
    
               The ``raise_error=False`` argument only affects the
               `HTTPError` raised when a non-200 response code is used,
               instead of suppressing all errors.
            """
    

    所以得像这样使用

        http_client = AsyncHTTPClient()
        future= http_client.fetch(url)
    
        future.add_done_callback(handle_response)
    

    https://www.tornadoweb.org/en/stable/httpclient.html

    评论

报告相同问题?