30

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%;
      }

etc etc.

Github のコンテンツですが、ファイルのコンテンツではありません。私は何を間違っていますか?

4

4 に答える 4

32

問題のファイルの内容は、返されたデータに含まれています。コンテンツだけでなく、そのファイルの完全な GitHub ビューを取得しています。

ファイルだけをダウンロードする場合Rawは、ページの上部にあるリンクを使用する必要があります (例として):

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

ドメイン名が変更されblob/、パスの一部がなくなっていることに注意してください。

これをrequestsGitHub リポジトリ自体で実証するには:

>>> 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. ...]
于 2013-01-02T10:41:39.280 に答える
9

からファイルの raw バージョンを要求する必要がありますhttps://raw.github.com

違いを見ます:

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

/また、おそらくディレクトリとファイル名の間に a を追加する必要があります。

>>> getcwd()+'foo.txt'
'/Users/burhanfoo.txt'
>>> import os
>>> os.path.join(getcwd(),'foo.txt')
'/Users/burhan/foo.txt'
于 2013-01-02T10:43:32.890 に答える