[英]Inputing multiple usernames from text file to run in python script
I have the following script that gets me the public data of a webpage of the username. 我有以下脚本,它获取用户名网页的公共数据。 I currently have it working for one username.
我目前有一个用户名。
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)
I would like to extract multiple usernames from a txt file (data.txt) and run it simultanerously. 我想从txt文件(data.txt)中提取多个用户名并同时运行它。 I am able to convert the text from the txt file into a list.
我能够将文本从txt文件转换为列表。
# Usernames extracted from text file:
base = [line.rstrip() for line in open('data.txt')]
Example text in txt file: txt文件中的示例文本:
janedoe
johnnyb
marcusx
taylors
But I'm not sure how to adjust my script to accept multiple usernames. 但我不知道如何调整我的脚本以接受多个用户名。 How do I do that?
我怎么做?
Put your current code in a function and run the function on each username in the base
list using, say, a list comprehension: 将当前代码放在函数中,然后使用列表推导对
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.