2013-05-13 29 views
0

我想創建一個腳本,查看文件,讀取每行(這是url)併爲我獲取HTTP標題。 我有問題: 我嘗試將結果重定向到文本文件,但無論如何,我嘗試,它不工作。Python 3.3 - 獲取標題並重定向結果

有人可以幫我用我的代碼嗎?

import urllib.request 
import sys 
open('sorti.txt','w') 
sorti = open("sorti.txt",'w') 
print('Creation de sorti.txt') 

text_file = open ("id.txt", "r") 
text_file.read().strip('\n') 
for lines in text_file: 
    urllib.request.urlopen('lines').write.sorti() 
    header = urllib.request.parse_http_list(lines).write.sorti() 
    sys.stdout(sorti) 
text_file.close 
sorti.close 
+0

你試過了嗎?它工作? – Salem 2013-06-03 18:19:41

回答

0

假如您正在尋找這樣的事情

URL1 
    header_1: value 
    ... 
    header_n: value 

    URL2 
    header_1: value 
    ... 
    header_n: value 

變化的代碼如下:

text_file = open ("id.txt", "r") 
for line in text_file: 
    sorti.write(line) // writes the current url 
    obj = urllib.request.urlopen(line) 
    headers = dict(obj.info()) // gets the headers 
    for (h,v) in headers.items(): // write all with the specified format 
     sorti.write("{0}: {1}\n".format(h, v)) 
    sorti.write("\n") 
sorti.close 

那些write.sorti()將無法​​正常工作。