2012-05-07 10 views
3
curl -L http://tumblr.com/tagged/long-reads 

これは結果である:http://pastebin.com/XtQVubBpCURLとURLLIB2を使用した場合、どうして私は異なる結果になるのですか?

応答が

def download(url): 
    f = urllib2.urlopen(url) 
    return f.read() 
html = download('http://tumblr.com/tagged/long-reads') 
print html 

とは異なることにこれが第二1の結果である:http://pastebin.com/MdzrhBZv

なぜ? curlと同じものを返すようにdownload()します。私は何をすべきか?

ここにCURLリクエストヘッダーがあります。

$ curl -v -L http://tumblr.com/tagged/long-reads 
* About to connect() to tumblr.com port 80 (#0) 
* Trying 50.97.149.179... connected 
* Connected to tumblr.com (50.97.149.179) port 80 (#0) 
> GET /tagged/long-reads HTTP/1.1 
> User-Agent: curl/7.21.6 (i686-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 librtmp/2.3 
> Host: tumblr.com 
> Accept: */* 
> 
< HTTP/1.1 301 Moved Permanently 
< Cache-Control: no-cache 
< Content-length: 0 
< Location: http://www.tumblr.com/tagged/long-reads 
< Connection: close 
< 
* Closing connection #0 
* Issue another request to this URL: 'http://www.tumblr.com/tagged/long-reads' 
* About to connect() to www.tumblr.com port 80 (#0) 
* Trying 50.97.143.18... connected 
* Connected to www.tumblr.com (50.97.143.18) port 80 (#0) 
> GET /tagged/long-reads HTTP/1.1 
> User-Agent: curl/7.21.6 (i686-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 librtmp/2.3 
> Host: www.tumblr.com 
> Accept: */* 
> 
< HTTP/1.1 200 OK 
< Date: Mon, 07 May 2012 22:09:01 GMT 
< Server: Apache 
< P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL" 
< Set-Cookie: tmgioct=iVajmrL8Wj8YffLTthjFyqYn; expires=Thu, 05-May-2022 22:09:01 GMT; path=/; httponly 
< Vary: Accept-Encoding 
< X-Tumblr-Usec: D=266934 
< Connection: close 
< Transfer-Encoding: chunked 
< Content-Type: text/html; charset=UTF-8 
< 

編集:私は今私の問題を解決し、誰でもTO 500 BOUNTYを提供します。

+0

両方のドキュメントをよく読んでください。 curl/urllib2での私の経験は、curlがurllib2がしないような見出しやヘッダを持つ最高の推測の魔法をたくさんしているということでした。特にコンテンツタイプや実際のHTTPメド(GET/PUT/etc)のようなものの周り。 –

+1

違いは何ですか? – dbf

+0

curlと同じものを返すにはどうすればよいですか? – TIMEX

答えて

0

どのように同じように見えるかを正確に知ることは困難です。 curlがどのヘッダーを使用しているのかを知り、urllib2に再現する必要があります。あなたは、ヘッダーcurl用途を知っていればしかし、それはRequestオブジェクトでこれらのヘッダを設定するのと同じくらい簡単でなければなりません:

>>> moz_req = urllib2.Request('http://www.google.com', headers={'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'}) 
>>> pyt_req = urllib2.Request('http://www.google.com', headers={'User-Agent': 'Python-urllib/2.6'}) 
>>> moz_url = urllib2.urlopen(moz_req)           
>>> moz_str = moz_url.read() 
>>> moz_url.close() 
>>> pyt_url = urllib2.urlopen(pyt_req) 
>>> pyt_str = pyt_url.read() 
>>> pyt_url.close() 
>>> moz_str == pyt_str 
False 

私は次のことを行うと、私はブログの記事の完全なページを取得します。

import urllib2 

def download(url): 
    headers = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'} 
    req = urllib2.Request(url, headers=headers) 
    url = urllib2.urlopen(req) 
    page = url.read() 
    url.close() 
    return page 

html = download('http://tumblr.com/tagged/long-reads') 
page = open('page.html', 'w') 
page.write(html) 
page.close() 

しかし、私はチェックし、ヘッダーを設定しなくても同じ結果が得られます。他の何かが間違っています...

+0

もしできれば、私はあなたにもう一度+1します。 :) –

+0

@sendle私はあなたのコードを使用しましたが、私はまだブログの投稿を取得しませんでした。奇妙な。私はEC2にいる。 IPはブロックできますか?しかし、IPがブロックされていると、同じマシンからcURLが動作するのはなぜですか? – TIMEX

+0

暗いところで撮影しましたが、あなたのCLIユーザーとは違って、あなたのPythonインタプリタが(違うpermsと制限を使って)別のユーザーの下で動作している可能性がありますか? –

関連する問題