“TypeError:当使用txt文件作为输入时,只能将str(而不是“列表”)连接到str”

2024-09-27 19:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个python项目来检查Ubisoft.com的用户名可用性。我通过向UbisoftAPI发出GET请求来实现这一点。要检查可用性的用户名存储在.txt文件中

当我运行代码时,我得到一个回溯错误,说:

File "C:\Users\Gibbo\OneDrive\Bureaublad\Gxzs uPlay\test.py", line 24, in check
    r = requests.get("https://public-ubiservices.ubi.com/v2/profiles?nameOnPlatform=" + username + "&platformType=uplay", headers = {
TypeError: can only concatenate str (not "list") to str

守则:

import requests
import json
from discord_webhook import DiscordWebhook, DiscordEmbed
import pyfiglet
from pyfiglet import figlet_format
import colorama
from colorama import Fore, Style

colorama.init()

def title():
    print(figlet_format("Gxzs's Checker!"))
    print(Fore.CYAN + 'Contact me on Discord Gxzs#3448 for Inquiries' + Fore.CYAN)

title()


def check():
    colorama.init()
    token = input("Enter token > ")
    
    username = [line.strip() for line in open("usernames.txt")]
    
    r = requests.get("https://public-ubiservices.ubi.com/v2/profiles?nameOnPlatform=" + username + "&platformType=uplay", headers = {
                'Method':'GET',
                'Authority':'public-ubiservices.ubi.com',
                'Ubi-AppId':'880650b9-35a5-4480-8f32-6a328eaa3aad',
                'Authorization': token,
                'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
                'Ubi-RequestedPlatformType':'uplay',
                })   
      
    userWasFound = len(r.json()['profiles']) != 0
    for i in range(5):
        print(" ")
    if userWasFound == False:
        print(username + Fore.WHITE + ' is available! :)' + Fore.WHITE)
        with open("available.txt", 'a') as f:
            f.write(f"{username}\n")
            
        webhook = DiscordWebhook(url='https://discord.com/api/webhooks/845031037838688297/N8cjHEwfhoYV-kWgSCA8L5vpMP8C32RsuPz29rTawAx7qtXqzlTV5DNuD7cKYPSSkWbw')
        embed = DiscordEmbed(title="Gxzs' Ubisoft Service", description= username + " is available or restricted", color='03b2f8')
        webhook.add_embed(embed)
        embed.set_image(url='https://media2.giphy.com/media/y8fXirTJjj6E0/giphy.gif?cid=ecf05e47foeqh9oalc5del7633idlrw3jg0jp7jo3fs7o8j3&rid=giphy.gif&ct=g')
        response = webhook.execute()
        username="Gxzs' uPlay Bot",
        avatar_url="https://i.4cdn.org/soc/1620674149395.png"

    if userWasFound == True:
        print(username + Fore.WHITE + ' is taken :( ' + Fore.WHITE)

print(Fore.CYAN + "Press ENTER to start the process..." + Fore.CYAN)
input("")
check()


我已经尝试让代码只检查输入中的一个用户名username = input("What username to check? > ") 我尝试过使用不同的方法导入.txt文件,比如usernames = open('usernames.txt','r').read().splitlines()


Tags: httpsimporttxtcomcheckusernameembedwebhook
2条回答

获取列表中的元素,而不是列表中的元素

本例采用列表的第一个元素

username = open('usernames.txt','r').read().splitlines()[0]

这将打印出一个带有前面数字的用户名列表,因此您可以要求用户输入这个数字,然后根据用户输入的username = usernames[user_input_int]获取元素

    usernames = [line.strip() for line in open("usernames.txt")]
    for index, username in enumerate(usernames):
        print(f'{index:>3}: {username}')

我强烈建议你通读w3 docs中的列表

您可以将其描述为在要检查的txt文件中有多个LPE用户名。看起来输出是一个用户名列表,但是,get实现假设它是一个字符串而不是一个列表。因此,要检查每个用户名,请执行以下操作:

for name in username:
    r = requests.get("https://public-ubiservices.ubi.com/v2/profiles?nameOnPlatform=" + name + "&platformType=uplay", headers ......   
  
    userWasFound = len(r.json()['profiles']) != 0
    if userWasFound:
        break

或者在找到用户名时您想要的任何行为。这样会检查txt文件中的每个用户名,直到找到用户名为止

相关问题 更多 >

    热门问题