如何解决“TypeError:使用txt文件作为输入时,只能将str不是“list”连接到str”
我正在开发一个 Python 项目,以检查 Ubisoft.com 的用户名可用性。我通过向 Ubisoft API 发出 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()
我尝试让代码只检查 Input username = input("What username to check? > ")
中的一个用户名
我尝试使用不同的方法导入 .txt 文件,例如 usernames = open('usernames.txt','r').read().splitlines()
解决方法
您将其描述为您要检查的 txt 文件中有多个用户名。看起来输出是一个用户名列表,但是,您的 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 文件中的每个用户名都会被检查,直到找到用户名。
,获取列表中的元素而不是列表。
此示例采用列表的第一个元素。
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列表
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。