python
  • image
  • download
  • urllib
  • python-3.5
  • 2016-07-21 3 views 0 likes 
    0

    URLを変更して画像をダウンロードしようとしましたが、エラーが発生しました。Pythonは、変数を変更することで画像をダウンロードします

    url_image="http://www.joblo.com/timthumb.php?src=/posters/images/full/"+str(title_2)+"-poster1.jpg&h=333&w=225" 
    
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' 
    headers = {'User-Agent': user_agent} 
    req = urllib.request.Request(url_image, None, headers) 
    
    
    print(url_image) 
    #image, h = urllib.request.urlretrieve(url_image) 
    with urllib.request.urlopen(req) as response: 
        the_page = response.read() 
    
    #print (the_page) 
    
    
    with open('poster.jpg', 'wb') as f: 
        f.write(the_page) 
    

    トレースバック(最新の呼び出しの最後): ファイル "C:\ Users \ユーザールーク\デスクトップ\スクレーパー\イメージャfinder.py" urllib.request.urlopen(REQ)と では、ライン97、などレスポンス: ファイル "C:\ Users \ luke \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ urllib \ request.py"、行162、urlopen return opener.open(url、data、timeout) ファイル "C:¥Users¥luke¥AppData¥Local¥Programs¥Python¥Python35-32¥lib¥urllib¥request.py"、行465、オープン中 応答= self._open(req、data) ファイル " C:\ Users \ luke \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ urllib \ request.py "、行483、_open '_open'、req) ファイル "C:¥Users¥luke¥AppData¥Local¥Programs¥Python¥Python35-32¥lib¥urllib¥request.py"、行443、_call_chain 結果= func(* args ) http_openのファイル "C:¥Users¥luke¥AppData¥Local¥Programs¥Python¥Python35-32¥lib¥urllib¥request.py"、1268行、 return self.do_open(http.client.HTTPConnection、req ) do_openのファイル "C:\ Users \ luke \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ urllib \ request.py" 1243行do_open内 r = h.getresponse() ファイル "C: \ Users \ユーザールーク\のAppData \ローカル:C "のGetResponse response.begin() ファイルでは、行1174、" \ Users \ユーザールーク\のAppData \ローカル\プログラム\ Pythonの\ Python35-32 \ libに\ HTTP \ client.py \ Program \ Python \ Python35-32 \ lib \ http \ client.py "、行282、begin バージョン、ステータス、理由= self._read_status() ファイル "C:\ Users \ユーザールーク\のAppData \ローカル\プログラム\ Pythonの\ Python35-32 \ libに\ HTTP \ client.py" _read_status レイズでは、ライン264、 BadStatusLine(行) http.client.BadStatusLine:

    +0

    をurllib2の使用方法を扱ういくつかの他の質問をグーグルその場合には、何らかの理由でWindows上でMozillaを使用していますか?)。また、title_2が何であるかわからない。奇数の文字やスペースがある場合は、エンコードする必要があります。 – Fhaab

    +0

    はちょうどタイトル2のために10-クローバーフィールドレーンを使用してヘッダーなしで同じエラーメッセージがアップします – spark

    答えて

    0

    私の助言は、urlib2を使用することです。また、サーバーがサポートしている場合、gzipエンコード(帯域幅を減らす)も可能にする素晴らしい機能を書いています。私はソーシャルメディアファイルのダウンロードにこれを使用しますが、何かのために働く必要があります。

    私はあなたのコードをデバッグしようとしますが、単なるスニペットであり、エラーメッセージはひどくフォーマットされているので、エラーがどこで発生しているのかを正確に知るのは難しいです(コードスニペットの行97は確かにありません)。

    これは可能な限り短くはありませんが、明確で再利用可能です。これは3を使用しているように見える、のpython 2.7である - あなたは、ヘッダーなしで試してみてください(またはサーバはあなたがする必要がないpythonで3

    import urllib2 
    import gzip 
    from StringIO import StringIO 
    
    def download(url): 
        """ 
        Download and return the file specified in the URL; attempt to use 
        gzip encoding if possible. 
        """ 
        request = urllib2.Request(url) 
        request.add_header('Accept-Encoding', 'gzip') 
        try: 
         response = urllib2.urlopen(request) 
        except Exception, e: 
         raise IOError("%s(%s) %s" % (_ERRORS[1], url, e)) 
        payload = response.read() 
        if response.info().get('Content-Encoding') == 'gzip': 
         buf = StringIO(payload) 
         f = gzip.GzipFile(fileobj=buf) 
         payload = f.read() 
        return payload 
    
    def save_media(filename, media): 
        file_handle = open(filename, "wb") 
        file_handle.write(media) 
        file_handle.close() 
    
    title_2 = "10-cloverfield-lane" 
    media = download("http://www.joblo.com/timthumb.php?src=/posters/images/full/{}-poster1.jpg&h=333&w=225".format(title_2)) 
    save_media("poster.jpg", media) 
    
    関連する問題