2017-02-23 75 views
0

如何從.txt文件中讀取包含所述3個URL的多行,「捲曲」它們以提取HTML並將它們全部輸出到它們各自的python文件中? 我tried-在Python中捲曲並輸出到單獨的文本文件

import commands 
    import os 
    import json 

    # RAW DATA 
    input = open('uri.txt', 'r') 
    t_1 = open('command', 'w') 
    counter_1 = 0 

    for line in input: 
      counter_1 += 1 
    if counter_1 < 3: 
      filename = str(counter_1) 
      print str(line) 
    filename= str(count) 
    command ='curl ' + '"' + str(line).rstrip('\n') + '"'+ '> ./rawData/' + filename 

    output_1 = commands.getoutput(command) 
    input.close() 

當我跑我的PyCharm的代碼,它會在我的目錄中的一個空白文件名爲「FILENAME.TXT」

+0

StackOverflow不是一個編碼器出租服務。你試過什麼了?你有沒有對你的問題做過任何研究?你有沒有遇到特定的障礙?請將所有這些信息添加到您的問題。 – KernelPanic

+0

@KernelPanic我已經嘗試了一些東西,我在之前的問題中發佈了我的代碼。我將在這裏添加它 – BCarey

+0

編碼器出租lol – BCarey

回答

0

你應該從蟒蛇內使用requests,而不是捲曲,因爲這樣可以更輕鬆地處理響應。此外,使用文件的規範方式是使用上下文管理器:with open(filename) as f

+1

謝謝!我也會嘗試這個! – BCarey