我对Python很新,所以我希望有人可以帮我根据文本文件中的信息生成一个唯一的URL列表。
示例:我有基本URL,www.website.com / users /,以及带有用户名,'frank','rachel','james'等的txt文件。我想创建包含此信息的URL ,并将其保存为txt文件,如下所示:
www.website.com/users/frank
www.website.com/users/rachel
www.website.com/users/james
etc.
我做过与数字类似的事情,例如:
www.website.com/1
www.website.com/2
etc.
我为数字解决方案编写的代码粘贴在下面,以防它作为起点有用。
import time
htmlTxt=""
pageNum=0
x="http://forum.com/eforum/forumdisplay.php?fid=13&page="
y=x+str(pageNum)
file = open("URLs.txt", "wb")
while True:
try:
time.sleep(0.001) # do something here
file.write(x +str(pageNum)+"\n")
pageNum+=1
except KeyboardInterrupt:
print '\nPausing... (Hit ENTER to continue, type quit to exit.)'
try:
response = raw_input()
if response == 'quit':
break
print 'Resuming...'
except KeyboardInterrupt:
print 'Resuming...'
continue
file.close()
(我在上面的示例中使用'time'的原因是因为我不知道如何让它停在某个数字上,所以我只是让它运行了几秒钟并删除了超出'max'数字。)
提前致谢!
答案 0 :(得分:1)
打开包含读取名称的文件,另一个包含输出的文件 - 用于写入。逐行读取输入文件并写入附加名称的输出:
URL = "www.website.com/users/"
with open('input.txt', 'r') as input_file:
with open('output.txt', 'w') as output_file:
for line in input_file:
output_file.write(URL + line)
对于包含以下内容的input.txt
:
frank
rachel
james
它产生以下output.txt
:
www.website.com/users/frank
www.website.com/users/rachel
www.website.com/users/james
答案 1 :(得分:1)
从用户名文件中读取一行,构造url并将其写入输出文件。
with open('usernames', 'r') as input_file, open('output', 'a') as output_file:
for line in input_file:
url = "http://website.com/{}".format(line.strip())
output_file.write(url)
input_file.close()
output_file.close()
答案 2 :(得分:1)
关于“如何让它停在某个数字上”:
您可以使用for
循环,它通常用于迭代列表:
for i in range(maxnumber):
# this body is executed maxnumber times and i is 0, 1, ..., maxnumber - 1
答案 3 :(得分:0)
这很好。
URL = "www.website.com/users/"
with open('input.txt', 'r') as input_file:
with open('output.txt', 'w') as output_file:
for line in input_file:
output_file.write(URL + line)
可以请您更新以下代码。因为此代码输出将转到单独的行。
URL = "www.website.com/users/"
URI_PART = "/set/passwd"
with open('input.txt', 'r') as input_file:
with open('output.txt', 'w') as output_file:
for line in input_file:
new_url = URL + line + URI_PATH
print(new_url)
然后,输出URL将分为两行。 你有这个主意吗?