0
我正在嘗試從10個URL的.txt文件中提取原始數據,並將每行(URL)的原始數據放在.txt文件中。然後通過使用Python重複處理過程中的數據(來自同一個原始.txt文件的原始數據被剝離了html)。從包含URL的.txt文件中再次下載數據
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 < 11:
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()
# PROCESSED DATA
counter_2 = 0
input = open('uri.txt','r')
t_2 = open('command','w')
for line in input:
counter_2 += 1
if counter_2 <11:
filename = str(counter_2) + '-processed'
command = 'lynx -dump -force_html ' + '"'+ str(line).rstrip('\n') + '"'+'> ./processedData/' + filename
print command
output_2 = commands.getoutput(command)
input.close()
我試圖用一個腳本來做所有這些事情。任何人都可以幫助我優化我的代碼,以便我可以運行它嗎?它應該在.txt文件中爲每種類型的行完全遍歷代碼。例如,我應該爲我的.txt文件中的每個url行處理1個原始文件& 1 .txt文件。
文件中有10行。我認爲這將從1,2,... 10開始計算。我打算把10. – BCarey
我的觀點是,如果你硬編碼的價值,那麼你將永遠無法處理具有不同數量的行的文件。您應該看看有多少行,並將其用作迭代次數。 –
我只需要用一個文件做一次,我知道它有多少行。我明白你來自哪裏。 – BCarey