2017-03-25 9 views
1

とURLを要求するとき、私はこのコードで、HTMLコンテンツを取得するためにはPython 3.6での要求を使用しています:正規化HTMLコンテンツの要求

import requests 
url = 'https://www.nytimes.com/2017/03/17/world/europe/trump-britain-obama-wiretap-gchq.html' 
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 
response = requests.get(url, headers=headers) 
print(response.content) 

ただし、出力は多くは「\ n」は文字で奇妙なコンテンツがあります。

b'<!DOCTYPE html>\n<!--[if (gt IE 9)|!(IE)]> <!--> <html lang="en" class="no-js section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" itemid="https://www.nytimes.com/2017/03/17/world/europe/trump-britain-obama-wiretap-gchq.html" itemtype="http://schema.org/NewsArticle" itemscope xmlns:og="http://opengraphprotocol.org/schema/"> <!--<![endif]-->\n<!--[if IE 9]> <html lang="en" class="no-js ie9 lt-ie10 section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" xmlns:og="http://opengraphprotocol.org/schema/"> <![endif]-->\n<!--[if IE 8]> <html lang="en" class="no-js ie8 lt-ie10 lt-ie9 section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" xmlns:og="http://opengraphprotocol.org/schema/"> 
<![endif]-->\n<!--[if (lt IE 8)]> <html lang="en" class="no-js lt-ie10 lt-ie9 lt-ie8 section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" xmlns:og="http://opengraphprotocol.org/schema/"> <![endif] 
-->\n<head>\n ..." 

完全な標準HTML出力を得るにはどうすればよいですか?

答えて

0

代わりresponse.contentの使用response.text - 以下に引用リクエストドキュメントに述べたように、これはHTTP応答によって提供される符号化情報を使用して、Unicode文字列に対する応答コンテンツを復号する。

content

レスポンスの内容(バイト単位)。

text

ユニコードでの応答のコンテンツ。

Response.encodingがNoneの場合、chardetを使用してエンコードが推測されます。

レスポンスコンテンツのエンコーディングは、HTTPヘッダーのみに基づいて決定され、RFC 2616の後にその文字があります。 HTTP以外の知識を利用してエンコーディングを推測する場合は、このプロパティにアクセスする前にr.encodingを適切に設定する必要があります。

例:

import requests 
url = 'https://www.nytimes.com/2017/03/17/world/europe/trump-britain-obama-wiretap-gchq.html' 
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 
response = requests.get(url, headers=headers) 
print(response.text) 

出力:

<!DOCTYPE html> 
<!--[if (gt IE 9)|!(IE)]> <!--> <html lang="en" class="no-js section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" itemid="https://www.nytimes.com/2017/03/17/world/europe/trump-britain-obama-wiretap-gchq.html" itemtype="http://schema.org/NewsArticle" itemscope xmlns:og="http://opengraphprotocol.org/schema/"> <!--<![endif]--> 
<!--[if IE 9]> <html lang="en" class="no-js ie9 lt-ie10 section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" xmlns:og="http://opengraphprotocol.org/schema/"> <![endif]--> 
<!--[if IE 8]> <html lang="en" class="no-js ie8 lt-ie10 lt-ie9 section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" xmlns:og="http://opengraphprotocol.org/schema/"> <![endif]--> 
<!--[if (lt IE 8)]> <html lang="en" class="no-js lt-ie10 lt-ie9 lt-ie8 section-europe format-medium tone-news app-article page-theme-standard has-comments has-top-ad type-size-small has-large-lede" xmlns:og="http://opengraphprotocol.org/schema/"> <![endif]--> 
<head> 
    <title>Trump Offers No Apology for Claim on British Spying - The New York Times</title> 
     <!-- etc ... --> 
</body> 
</html> 
+1

パーフェクト!できます。 –