我如何从 python 中的 txt 文件中请求每个令牌

我有一个名为 tokens.txt 的文本文件。


例如:12463,4126,6343,6345。


我想用每个令牌发送一个发布请求并使用多线程。


由于某些原因,我的代码只从 txt 文件中获取最后一个标记,并且只使用它。


import requests

from concurrent.futures import ThreadPoolExecutor, as_completed

from time import time


url_list = [

    "https://www.google.com/api/"

]

file_lines = open("tokens.txt", "r").readlines()

for line in file_lines:

    tokens = {

        'Token':line.replace('/n','')

        }


def makerequest(url):

    while True:

        html = requests.post(url,stream=True, data=tokens)

        print(tokens)

        return html.content


start = time()


processes = []

with ThreadPoolExecutor(max_workers=200) as executor:

    for url in url_list:

        processes.append(executor.submit(makerequest, url))


for task in as_completed(processes):

    print(task.result())



print(f'Time taken: {time() - start}')

我如何为每个令牌发送请求?


温温酱
浏览 114回答 3
3回答

沧海一幻觉

在你的情况下tokens = {"Token": <last_token>}像这样修改您的代码,以便可以为每个令牌发送一个请求。tokens = set()&nbsp;'''<- You can use list also but in this case set is better&nbsp; as it will ensure only&nbsp;one request for one token even if your tokens file contains duplicate line.'''url_list = [&nbsp; &nbsp; "https://www.google.com/api/"]tokens = set()with open("tokens.txt", "r") as f:&nbsp; &nbsp; file_lines = f.readlines()&nbsp; &nbsp; for line in file_lines:&nbsp; &nbsp; &nbsp; &nbsp; tokens.add(line.strip())token_data = {"Token": None}def makerequest(url):&nbsp; &nbsp; for token in tokens:&nbsp; &nbsp; &nbsp; &nbsp; token_data["Token"] = token&nbsp; &nbsp; &nbsp; &nbsp; html = requests.post(url,stream=True, data=token_data)&nbsp; &nbsp; &nbsp; &nbsp; print(token)&nbsp; &nbsp; &nbsp; &nbsp; # do something with html here&nbsp; &nbsp; &nbsp; &nbsp; # don't return or break

慕少森

你在做数据 = 令牌那一点tokens是最后一行的分配。如果你想要所有的令牌,你需要做一些像j这样的事情:tokens&nbsp;=&nbsp;set() for&nbsp;line&nbsp;file_lines: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokens.add(......)

婷婷同学_

您的代码的问题是令牌字典的创建 - 您循环提供令牌,但您总是覆盖映射到“令牌”键的值。此外,您的代码中还有一些不良做法。请像您一样小心内联打开文件file_lines = open("tokens.txt", "r").readlines()而是将其用作上下文管理器with&nbsp;open("tokens.txt",&nbsp;"r")&nbsp;as&nbsp;file: &nbsp;&nbsp;&nbsp;&nbsp;file_lines&nbsp;=&nbsp;file.readlines()这可以确保文件在您阅读后再次关闭 - 在您的情况下,您需要确保文件被关闭(即使在崩溃等情况下)其次,避免在函数中使用全局变量。根据你的代码,我假设你想用每个标记查询不同的 url - 所以函数应该接受这两个作为参数。然后我会分别创建一个组合列表,比如url_token_combs&nbsp;=&nbsp;[(url,&nbsp;token.strip())&nbsp;for&nbsp;url&nbsp;in&nbsp;url_list&nbsp;for&nbsp;token&nbsp;in&nbsp;file_lines]最后,更改您的功能以使用传递给它的参数而不是全局参数,例如:def&nbsp;makerequest(url_token&nbsp;): &nbsp;&nbsp;&nbsp;&nbsp;url&nbsp;,&nbsp;token&nbsp;=&nbsp;url_token&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;html&nbsp;=&nbsp;requests.post(url,stream=True,&nbsp;data=token)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;html.content这使您现在可以使用以下线程遍历代码:import requestsfrom concurrent.futures import ThreadPoolExecutor, as_completedfrom time import timedef makerequest(url_token):&nbsp; &nbsp; url , token = url_token&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; html = requests.post(url,stream=True, data=tokens)&nbsp; &nbsp; print(tokens)&nbsp; &nbsp; return html.contentif __name__ == "__main__":&nbsp; &nbsp; start = time()&nbsp; &nbsp; url_list = [&nbsp; &nbsp; "https://www.google.com/api/"&nbsp; &nbsp; ]&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; with&nbsp; open("tokens.txt", "r") as file:&nbsp; &nbsp; &nbsp; &nbsp; file_lines = file.readlines()&nbsp; &nbsp; tokens = [{'Token':line.replace('/n','') }for line in file_lines ]&nbsp; &nbsp; url_tokens = [(url, token.strip()) for url in url_list for token in tokens]&nbsp; &nbsp; processes = []&nbsp; &nbsp; with ThreadPoolExecutor(max_workers=200) as executor:&nbsp; &nbsp; &nbsp; &nbsp; for url_token in url_tokens:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processes.append(executor.submit(makerequest, url_token))&nbsp; &nbsp; for task in as_completed(processes):&nbsp; &nbsp; &nbsp; &nbsp; print(task.result())&nbsp; &nbsp; print(f'Time taken: {time() - start}')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python