[英]Inputing multiple usernames from text file to run in python script
我有以下脚本,它获取用户名网页的公共数据。 我目前有一个用户名。
usernames = ['johnsmith']
r = requests.get('https://url/'+username)
html = r.text.encode("utf-8")
text = html[html.index("htmlclass = ")+21:]
text = (text[:text.index("};</script>")]+"}").replace('\\"', "")
dictionary = loads(text)
我想从txt文件(data.txt)中提取多个用户名并同时运行它。 我能够将文本从txt文件转换为列表。
# Usernames extracted from text file:
base = [line.rstrip() for line in open('data.txt')]
txt文件中的示例文本:
janedoe
johnnyb
marcusx
taylors
但我不知道如何调整我的脚本以接受多个用户名。 我怎么做?
将当前代码放在函数中,然后使用列表推导对base
列表中的每个用户名运行该函数:
def get_username_data(username):
r = requests.get('https://url/'+username)
html = r.text.encode("utf-8")
text = html[html.index("htmlclass = ")+21:]
text = (text[:text.index("};</script>")]+"}").replace('\\"', "")
return loads(text)
result = [get_username_data(uname) for uname in base]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.