2013-01-02 21 views
13

比方說,有一種生活在GitHub庫寫入文件從GitHub文件:如何下載和使用要求

https://github.com/someguy/brilliant/blob/master/somefile.txt

我試圖使用請求來請求該文件,寫的內容將其存儲到當前工作目錄中的磁盤中,以供稍後使用。現在,我使用下面的代碼:

import requests 
from os import getcwd 

url = "https://github.com/someguy/brilliant/blob/master/somefile.txt" 
directory = getcwd() 
filename = directory + 'somefile.txt' 
r = requests.get(url) 

f = open(filename,'w') 
f.write(r.content) 

無疑醜,更重要的是,不能正常工作。而不是預期的文本,我得到:

<!DOCTYPE html> 
<!-- 

Hello future GitHubber! I bet you're here to remove those nasty inline styles, 
DRY up these templates and make 'em nice and re-usable, right? 

Please, don't. https://github.com/styleguide/templates/2.0 

--> 
<html> 
    <head> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
    <title>Page not found &middot; GitHub</title> 
    <style type="text/css" media="screen"> 
     body { 
     background: #f1f1f1; 
     font-family: "HelveticaNeue", Helvetica, Arial, sans-serif; 
     text-rendering: optimizeLegibility; 
     margin: 0; } 

     .container { margin: 50px auto 40px auto; width: 600px; text-align: center; } 

     a { color: #4183c4; text-decoration: none; } 
     a:visited { color: #4183c4 } 
     a:hover { text-decoration: none; } 

     h1 { letter-spacing: -1px; line-height: 60px; font-size: 60px; font-weight: 100; margin: 0px; text-shadow: 0 1px 0 #fff; } 
     p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; } 

     ul { list-style: none; margin: 25px 0; padding: 0; } 
     li { display: table-cell; font-weight: bold; width: 1%; } 
     #error-suggestions { font-size: 14px; } 
     #next-steps { margin: 25px 0 50px 0;} 
     #next-steps li { display: block; width: 100%; text-align: center; padding: 5px 0; font-weight: normal; color: rgba(0, 0, 0, 0.5); } 
     #next-steps a { font-weight: bold; } 
     .divider { border-top: 1px solid #d5d5d5; border-bottom: 1px solid #fafafa;} 

     #parallax_wrapper { 
     position: relative; 
     z-index: 0; 
     } 
     #parallax_field { 
     overflow: hidden; 
     position: absolute; 
     left: 0; 
     top: 0; 
     height: 370px; 
     width: 100%; 
     } 

等等等等

從GitHub的內容,而不是文件的內容。我究竟做錯了什麼?

+2

你真的應該使用'os.path中。 join()'合併路徑。 'getcwd()'不一定返回以路徑分隔符結尾的目錄名稱。 –

回答

9

有問題的文件的內容是包括在返回的數據中的。您將獲得該文件的完整GitHub視圖,而不僅僅是內容。

如果你想下載只是文件,你需要使用Raw鏈接在網頁的頂部,這將是(對你的例子):

https://raw.github.com/someguy/brilliant/master/somefile.txt 

注意域的變化名稱,並且blob/部分路徑不見了。

>>> import requests 
>>> r = requests.get('https://github.com/kennethreitz/requests/blob/master/README.rst') 
>>> 'Requests:' in r.text 
True 
>>> r.headers['Content-Type'] 
'text/html; charset=utf-8' 
>>> r = requests.get('https://raw.github.com/kennethreitz/requests/master/README.rst') 
>>> 'Requests:' in r.text 
True 
>>> r.headers['Content-Type'] 
'text/plain; charset=utf-8' 
>>> print r.text 
Requests: HTTP for Humans 
========================= 


.. image:: https://travis-ci.org/kennethreitz/requests.png?branch=master 
[... etc. ...] 
+0

打我吧! :)(但是,有什麼新的!)(在一個側面說明 - 猜測這將是可能的使用'pygit' - 這可能是一個更有用/靈活的方式爲OP將來的事情) –

+0

@JonClements :'pygit'是相當低的水平,但是,不是嗎? –

+0

解決了 - 也感謝關於文件/目錄名加入的正確方法。一旦定時器到期,接受。 – Fomite

3

您需要請求文件的原始版本,從https://raw.github.com

爲了與requests GitHub的庫本身證明這一點。

看到區別:

https://raw.github.com/django/django/master/setup.pyhttps://github.com/django/django/blob/master/setup.py

而且,很可能需要添加一個/目錄和文件名之間:

>>> getcwd()+'foo.txt' 
'/Users/burhanfoo.txt' 
>>> import os 
>>> os.path.join(getcwd(),'foo.txt') 
'/Users/burhan/foo.txt' 
+0

這是一個比公認的答案簡單得多的實現,並且完全適合我。謝謝! – dslosky