I am writing a script parser in Python that should collect the entire message history from the conversation. I plan to implement this idea through the execute method (library "vk") and there was a question about passing arguments to the execute code. Can I somehow transfer the offset parameter to code?

reponse_get_history = apivk.messages.getHistory(peer_id=2000000590,offset=0,count=200,v=5.92) response_execute = [] count_message = 0 while count_message < reponse_get_history["count"]: code = ''' var i = 0; var response_get_history = []; var response_get_history_message = []; var offset = 0; while (i < 25) { response_get_history = response_get_history_message + API.messages.getHistory({"peer_id": 2000000590, "offset": offset, "count": 200}); response_get_history_message = response_get_history_message + response_get_history.items; i = i + 1; offset = offset + 200; } return response_get_history_message; ''' response_execute = response_execute + apivk.execute(code=code, v=5.92) count_message = count_message + len(response_execute) print(response_execute) time.sleep(50) 

    1 answer 1

    Do you want to change the value of the offset variable in the code line? This can be done by formatting the string. To do this, you must mark the space in the string, which then must be filled with other values ​​using the format() method. Places to fill are marked with curly brackets {} .

    Notice the var offset = {}; - this is the part that will be filled with new values ​​when formatting. You also need to escape curly brackets that are not areas for formatting and just need to be displayed in the summary line - {{ and }} .

     code = ''' var i = 0; var response_get_history = []; var response_get_history_message = []; var offset = {}; while (i < 25) {{ response_get_history = response_get_history_message + API.messages.getHistory({{"peer_id": 2000000590, "offset": offset, "count": 200}}); response_get_history_message = response_get_history_message + response_get_history.items; i = i + 1; offset = offset + 200; }} return response_get_history_message; ''' offset = 13 print(code.format(offset))