Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过python将json数据以相反的顺序发送到网站_Python_Json_Python Asyncio - Fatal编程技术网

通过python将json数据以相反的顺序发送到网站

通过python将json数据以相反的顺序发送到网站,python,json,python-asyncio,Python,Json,Python Asyncio,所以我一直在做一个小项目,作为一个学习经验,这个概念一开始只是朋友之间的一个玩笑 其思想是python将以与.json文件相反的顺序处理数据,然后将每1000个字符作为注释发送到限制为1000个字符的网站 目前,它的工作预期,但当打印的评论出现了重复的顺序。那么会是什么呢 将显示为 我已经隔离了我认为可能存在的问题,但不理解为什么会发生这个问题。任何反馈都会很好,提前谢谢 可能的问题1: script_path = 'script.json' with open(script_path, 'r'

所以我一直在做一个小项目,作为一个学习经验,这个概念一开始只是朋友之间的一个玩笑

其思想是python将以与.json文件相反的顺序处理数据,然后将每1000个字符作为注释发送到限制为1000个字符的网站

目前,它的工作预期,但当打印的评论出现了重复的顺序。那么会是什么呢

将显示为

我已经隔离了我认为可能存在的问题,但不理解为什么会发生这个问题。任何反馈都会很好,提前谢谢

可能的问题1:

script_path = 'script.json'
with open(script_path, 'r') as f:
  script = json.loads(f.read())

def reverse(l):
  size = len(l)
  nindex = size - 1
  ll = int(size/2)
  for i in range(0, ll):
    temp = l[nindex]
    l[nindex] = l[i] 
    l[i] = temp
    nindex -= 1

i = 0
count = 0
script_small_parts = []
script_parts = {}
for x in script:
  if i > 10:
    i = 0
    script_parts.update({count:script_small_parts})
    count += 1
    script_small_parts = []
    script_small_parts.append(x)
  script_small_parts.append(x)
  i += 1
  if count == 179:
    script_parts.update({count:script_small_parts})
可能的问题2:

async def main():
  message = ''
  count = 0
  reverse(script_parts)
  for x in script_parts:
    for s in script_parts[x]:
      if len(s) < 1000 and len(message) < 800:
        message += s
      else:
        payload = {'comment': message, 'count': '6', 'sessionid': session_id}
        try:
          r = requests.post(api, headers=headers, cookies=cookies, data=payload)
          if r.json()['success'] == 'false':
            print("Something has gone wrong.")
            return
        except Exception as e:
          print("Something has gone wrong.")
          return
        message = ''
        message += str(s)
        count += 1
        print('Comment {0}/{1} Sent\nDelay: {2} seconds'.format(count, len(script_parts), delay))
        await asyncio.sleep(delay)
  last_comment = 'The test is now over'
  payload = {'comment': last_comment, 'count': '6', 'sessionid': session_id}
  r = requests.post(api, headers=headers, cookies=cookies, data=payload)
  print("done")
async def main():
消息=“”
计数=0
反向(脚本部分)
对于脚本中的x\u部分:
对于脚本中的s_部分[x]:
如果len(s)<1000且len(message)<800:
消息+=s
其他:
有效负载={'comment':消息'count':'6','sessionid':会话id}
尝试:
r=请求.post(api,headers=headers,cookies=cookies,data=payload)
如果r.json()
打印(“出了问题。”)
返回
例外情况除外,如e:
打印(“出了问题。”)
返回
消息=“”
消息+=str(s)
计数+=1
打印('Comment{0}/{1}已发送\n显示:{2}秒'。格式(计数、len(脚本部分)、延迟))
等待异步睡眠(延迟)
last_comment='测试现在结束'
有效负载={'comment':最后一条注释,'count':'6','sessionid':会话id}
r=请求.post(api,headers=headers,cookies=cookies,data=payload)
打印(“完成”)
script_path = 'script.json'
with open(script_path, 'r') as f:
  script = json.loads(f.read())

def reverse(l):
  size = len(l)
  nindex = size - 1
  ll = int(size/2)
  for i in range(0, ll):
    temp = l[nindex]
    l[nindex] = l[i] 
    l[i] = temp
    nindex -= 1

i = 0
count = 0
script_small_parts = []
script_parts = {}
for x in script:
  if i > 10:
    i = 0
    script_parts.update({count:script_small_parts})
    count += 1
    script_small_parts = []
    script_small_parts.append(x)
  script_small_parts.append(x)
  i += 1
  if count == 179:
    script_parts.update({count:script_small_parts})
async def main():
  message = ''
  count = 0
  reverse(script_parts)
  for x in script_parts:
    for s in script_parts[x]:
      if len(s) < 1000 and len(message) < 800:
        message += s
      else:
        payload = {'comment': message, 'count': '6', 'sessionid': session_id}
        try:
          r = requests.post(api, headers=headers, cookies=cookies, data=payload)
          if r.json()['success'] == 'false':
            print("Something has gone wrong.")
            return
        except Exception as e:
          print("Something has gone wrong.")
          return
        message = ''
        message += str(s)
        count += 1
        print('Comment {0}/{1} Sent\nDelay: {2} seconds'.format(count, len(script_parts), delay))
        await asyncio.sleep(delay)
  last_comment = 'The test is now over'
  payload = {'comment': last_comment, 'count': '6', 'sessionid': session_id}
  r = requests.post(api, headers=headers, cookies=cookies, data=payload)
  print("done")