python - 将数据从 Scrapy 传递到 Socket
问题描述
目前我有一个用于从网站抓取数据的scrapy。我想将当前数据发送到它使用套接字抓取的连接客户端。
clients = set()
clients_lock = threading.Lock()
host = socket.gethostbyname("")
port = 10010
def listener(client, address, item):
print ("Accepted connection from: ", address)
with clients_lock:
clients.add(client)
try:
data = client.recv(1024)
if data == ('0').encode():
timestamp = datetime.datetime.now().strftime("%I:%M:%S %p")
client.send(timestamp.encode())
finally:
with clients_lock:
clients.remove(client)
client.close()
def send_to_socket(item):
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(3)
th = []
client, address = s.accept()
th.append(Thread(target=listener, args = (client,address, item)).start())
s.close()
下面给出的代码是调用启动套接字的函数的scrapy代码,当前的问题是我无法从客户端代码实时检索数据。当前代码每次发送数据时都会关闭客户端连接。有没有办法在不关闭客户端连接的情况下连续发送数据。如果我删除 client.remove(client) 代码会卡住并且不会运行 _build_link_item。
_build_link_item 连续运行以从不同的 URL 生成数据,我想将它生成的数据发送到客户端。
def _build_link_item(response):
"""Builds a LinkItem object for the given response"""
parser = HTMLParser(response)
item = LinkItem(
url=response.url,
status=response.status,
content_type=response.headers.get("Content-Type", b"").decode("utf-8") or "",
)
print(json.dumps(item.__dict__), flush=True)
send_to_socket(json.dumps(item.__dict__))
解决方案
推荐阅读
- c - 对象在 sdl 中自动移动
- php - 如何在 Adobe Brackets 上的实时预览中运行 php 文件?
- excel - 如何使用 IF 条件将单元格值映射到另一个单元格
- spring - 限制 Springboot 服务的上传/出站速度
- azure-data-explorer - 类型“数据库”的错误实体名称“N/A”不存在
- javascript - 如果不是`/user`,则反应路由器v4匹配路径
- java - Apache Spark SQL(DataSet) 转换为 DataFrame(用于 kmeans)
- excel - 在excel中,当存在空单元格时,无法获取行中最小值的列标题
- reactjs - React Redux:用另一个记录更新和替换记录返回值1
- scala - Scala 类型不匹配;找到:Int(0) 需要:Int