from kafka import KafkaProducer import json scrapy_kafka_hosts = ["ip1:端口", "ip2:端口"] topic = 'wangliang_test' # 主题 producer = KafkaProducer(bootstrap_servers=scrapy_kafka_hosts,retries=3, value_serializer=lambda v: json.dumps(v).encode('utf-8'), # 发送json 格式的消息 ) while True: name = input("输入你要name的内容") age = input("输入你要age的内容") msg = {"name":name,"age":age} producer.send(topic,msg)