2017-01-02 20 views
1

次のスクリプトを使用して、ユーザー名のWebページの公開データを取得します。私は現在、1つのユーザー名で作業しています。テキストファイルから複数のユーザー名を入力してPythonスクリプトで実行する

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 

しかし、私は複数のユーザ名を受け入れるように私のスクリプトを調整するかどうかはわかりません。それ、どうやったら出来るの?

答えて

0

、たとえば、リスト内包の機能であなたの現在のコードを入れて使用して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] 
関連する問題