2016-08-15 68 views
0

我有requests.get()的問題。 如果我手動將URL粘貼到代碼如下:requests.get返回<!doctype>而不是<Response>

r = requests.get('https://berlin.kauperts.de/Strassen/Igelsteig-12557-Berlin.html') 
print(r) 

我得到:<Response [200]>。這工作很好。 相反,如果我試圖索要請求出文件的列表,如:

indir = '/home/d/Desktop/civiv_hacking/Streetnames/dump/berlin.kauperts.de/Strassen' 
for root, dirs, filenames in os.walk(indir): 
    for z in filenames: 
    x = urlparse.urljoin('https://berlin.kauperts.de/Strassen/', z+'/'), 
    [t[0] for t in x], 
    print(t), 
    for r in requests.get(t):    
     print(r), 

我得到:https://berlin.kauperts.de/Strassen/Igelsteig-12557-Berlin/ <!DOCTYPE html> <html lang="de" xml:lang="de" xmlns="http://www.w3.org/1999/xhtml"> 我怎樣才能讓requests.get()返回<Response [200]>而不只是文檔類型的信息?

回答

1

如果你想看到的Response字符串表示,你不應該遍歷它,只是打印:

url = urlparse.urljoin('https://berlin.kauperts.de/Strassen/', z) 
response = requests.get(url) 
print(response) # would print "<Response [200]>" 
相關問題