[英]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.