使用 Python 请求模块自定义 GET 响应
Customizing GET Response Using Python Requests Module
我是 Python 的新手,请多多包涵...
尝试对 Instagram 进行简单的 oAuth 调用 API。注册应用程序后,您将获得客户端 ID、客户端密码等,oAuth 流程的第一步是将用户定向到此授权 URL:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code
当我使用我的客户端 ID 在浏览器中加载此 URL 并重定向 URL 时,以下 URL 出现在浏览器中(例如):
http://instagrram.geometryfletch.com/home.html?code=956237827314ee22092384984938
我的问题是,如何使用 Requests 模块复制浏览器中发生的情况?
当我尝试以下操作时:
>>> import requests
>>> b = requests.get('https://api.instagram.com/oauth/authorize/?client_id=c918883453360349850498&redirect_uri=http://instagrram.myredirect.com/home.html&response_type=code')
>>> b.text
我得到的是这个 "garbbled" 响应(我知道它并没有真正被抓取,Requests 正在做我告诉它的事情并且 return 正在做一些适当的事情):
u'<!DOCTYPE html>\n<!--[if lt IE 7]> <html lang="en" class="no-js lt-ie9 lt-ie8 lt-ie7 not-logged-in "> <![endif]-->\n<!--[if IE 7]> <html lang="en" class="no-js lt-ie9 lt-ie8 not-logged-in "> <![endif]-->\n<!--[if IE 8]> <html lang="en" class="no-js lt-ie9 not-logged-in "> <![endif]-->\n<!--[if gt IE 8]><!-->
<html lang="en" class="no-js not-logged-in "> <!--<![endif]-->\n
<head>\n
<meta charset="utf-8">
\n
<meta http-equiv="X-UA-Compatible" content="IE=edge">
\n\n <title>Log in — Instagram</title>\n\n
<script type="text/javascript">\
n
WebFontConfig = {
\
n
custom: {\n
families: [\'proxima-nova:n4,n7\'],\n urls: [\'//instagramstatic-a.akamaihd.net/bluebar/660508e/cache/styles/fonts.css\']\n }\n };\n</script>
\n
<script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/webfont.js" type="text/javascript"
async></script>
\n\n \n \n
<meta name="robots" content="noimageindex">
\n \n
<meta name="apple-mobile-web-app-capable" content="yes">
\n
<meta name="apple-mobile-web-app-status-bar-style" content="black">
\n\n\n \n
<meta id="viewport" name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1">
\n\n\n
<script type="text/javascript">\
n(function () {\n
var docElement = document.documentElement;\n
var classRE = new RegExp(\'(^|\\s)no-js(\\s|$)\');\n var className = docElement.className;\n docElement.className = className.replace(classRE, \'js\');\n })();\n </script>
\n\n \n\n \n \n \n
<link rel="Shortcut Icon" type="image/x-icon"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/favicon.ico">
\n \n \n
<link rel="apple-touch-icon-precomposed"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-precomposed.png">
\n
<link rel="apple-touch-icon-precomposed" sizes="72x72"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-72x72-precomposed.png">
\n
<link rel="apple-touch-icon-precomposed" sizes="114x114"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-114x114-precomposed.png">
\n
<link rel="apple-touch-icon-precomposed" sizes="144x144"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-144x144-precomposed.png">
\n \n \n
<link href="//instagramstatic-a.akamaihd.net/bluebar/660508e/cache/styles/distillery/dialog-main.css"
type="text/css" rel="stylesheet"></link>
\n
<!--[if lt IE 9]>\n <style>\n .dialog-outer {\n min-height: 0;\n }\n </style>\n
<![endif]-->\n\n \n
<script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/jquery.js" type="text/javascript"></script>
\n
<script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/bluebar.js" type="text/javascript"></script>
\n
<script type="text/javascript">\
n
$(document).ready(function () {\n
$("#id_username").focus();\n
setTimeout(function () {\n
document.getElementById(\'viewport\').setAttribute(\'content\', \'width=\'+ window.innerWidth + \', user-scalable=no\');\n }, 5);\n });\n</script>
\n\n\n
</head>
\n
<body class="p-dialog oauth-login">\n \n \n
<div class="root">\n \n
<section class="dialog-outer">\n
<div class="dialog">\n
<header>\n <h1 class="logo">Instagram</h1>\n \n</header>
\n
<div class="dialog-main">\n \n\n\n\n\n\n\n
<form method="POST" id="login-form" class="adjacent"
action="/accounts/login/?force_classic_login=&next=/oauth/authorize/?client_id=c91888345336494ab7ea7046427ca23e%26redirect_uri=http://instagrram.geometryfletch.com/home.html%26response_type=code">
\n <input type="hidden" name="csrfmiddlewaretoken" ..........
但是,当您将 URL 加载到浏览器时,我如何才能仅通过 code:code=956237827314ee22092384984938
获得对 return 的请求?
出于生产目的,您不应该 re-implement oauth。请看一下https://pypi.python.org/pypi/oauthlib which is an established library for performing the oauth authentication logic. If you want to stick with requests
, then there also is https://github.com/requests/requests-oauthlib。除此之外,关于你的问题
My question is, how can I replicate what happens in the browser using
the Requests module?
这不是微不足道的。首先,使用 curl
或 debugging/reconstructing 协议流的浏览器插件。然后第二步是使用 requests
.
重建相同的流
示例:当通过 GET 访问您在问题中提到的第一个 URL 时,服务器响应 302 redirection
,其目标在响应的 Location
字段中给出 header。响应还通过 Set-Cookie
header 字段设置一个 cookie。所有这些都很重要。
我是 Python 的新手,请多多包涵... 尝试对 Instagram 进行简单的 oAuth 调用 API。注册应用程序后,您将获得客户端 ID、客户端密码等,oAuth 流程的第一步是将用户定向到此授权 URL:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code
当我使用我的客户端 ID 在浏览器中加载此 URL 并重定向 URL 时,以下 URL 出现在浏览器中(例如):
http://instagrram.geometryfletch.com/home.html?code=956237827314ee22092384984938
我的问题是,如何使用 Requests 模块复制浏览器中发生的情况?
当我尝试以下操作时:
>>> import requests
>>> b = requests.get('https://api.instagram.com/oauth/authorize/?client_id=c918883453360349850498&redirect_uri=http://instagrram.myredirect.com/home.html&response_type=code')
>>> b.text
我得到的是这个 "garbbled" 响应(我知道它并没有真正被抓取,Requests 正在做我告诉它的事情并且 return 正在做一些适当的事情):
u'<!DOCTYPE html>\n<!--[if lt IE 7]> <html lang="en" class="no-js lt-ie9 lt-ie8 lt-ie7 not-logged-in "> <![endif]-->\n<!--[if IE 7]> <html lang="en" class="no-js lt-ie9 lt-ie8 not-logged-in "> <![endif]-->\n<!--[if IE 8]> <html lang="en" class="no-js lt-ie9 not-logged-in "> <![endif]-->\n<!--[if gt IE 8]><!-->
<html lang="en" class="no-js not-logged-in "> <!--<![endif]-->\n
<head>\n
<meta charset="utf-8">
\n
<meta http-equiv="X-UA-Compatible" content="IE=edge">
\n\n <title>Log in — Instagram</title>\n\n
<script type="text/javascript">\
n
WebFontConfig = {
\
n
custom: {\n
families: [\'proxima-nova:n4,n7\'],\n urls: [\'//instagramstatic-a.akamaihd.net/bluebar/660508e/cache/styles/fonts.css\']\n }\n };\n</script>
\n
<script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/webfont.js" type="text/javascript"
async></script>
\n\n \n \n
<meta name="robots" content="noimageindex">
\n \n
<meta name="apple-mobile-web-app-capable" content="yes">
\n
<meta name="apple-mobile-web-app-status-bar-style" content="black">
\n\n\n \n
<meta id="viewport" name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1">
\n\n\n
<script type="text/javascript">\
n(function () {\n
var docElement = document.documentElement;\n
var classRE = new RegExp(\'(^|\\s)no-js(\\s|$)\');\n var className = docElement.className;\n docElement.className = className.replace(classRE, \'js\');\n })();\n </script>
\n\n \n\n \n \n \n
<link rel="Shortcut Icon" type="image/x-icon"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/favicon.ico">
\n \n \n
<link rel="apple-touch-icon-precomposed"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-precomposed.png">
\n
<link rel="apple-touch-icon-precomposed" sizes="72x72"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-72x72-precomposed.png">
\n
<link rel="apple-touch-icon-precomposed" sizes="114x114"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-114x114-precomposed.png">
\n
<link rel="apple-touch-icon-precomposed" sizes="144x144"
href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-144x144-precomposed.png">
\n \n \n
<link href="//instagramstatic-a.akamaihd.net/bluebar/660508e/cache/styles/distillery/dialog-main.css"
type="text/css" rel="stylesheet"></link>
\n
<!--[if lt IE 9]>\n <style>\n .dialog-outer {\n min-height: 0;\n }\n </style>\n
<![endif]-->\n\n \n
<script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/jquery.js" type="text/javascript"></script>
\n
<script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/bluebar.js" type="text/javascript"></script>
\n
<script type="text/javascript">\
n
$(document).ready(function () {\n
$("#id_username").focus();\n
setTimeout(function () {\n
document.getElementById(\'viewport\').setAttribute(\'content\', \'width=\'+ window.innerWidth + \', user-scalable=no\');\n }, 5);\n });\n</script>
\n\n\n
</head>
\n
<body class="p-dialog oauth-login">\n \n \n
<div class="root">\n \n
<section class="dialog-outer">\n
<div class="dialog">\n
<header>\n <h1 class="logo">Instagram</h1>\n \n</header>
\n
<div class="dialog-main">\n \n\n\n\n\n\n\n
<form method="POST" id="login-form" class="adjacent"
action="/accounts/login/?force_classic_login=&next=/oauth/authorize/?client_id=c91888345336494ab7ea7046427ca23e%26redirect_uri=http://instagrram.geometryfletch.com/home.html%26response_type=code">
\n <input type="hidden" name="csrfmiddlewaretoken" ..........
但是,当您将 URL 加载到浏览器时,我如何才能仅通过 code:code=956237827314ee22092384984938
获得对 return 的请求?
出于生产目的,您不应该 re-implement oauth。请看一下https://pypi.python.org/pypi/oauthlib which is an established library for performing the oauth authentication logic. If you want to stick with requests
, then there also is https://github.com/requests/requests-oauthlib。除此之外,关于你的问题
My question is, how can I replicate what happens in the browser using the Requests module?
这不是微不足道的。首先,使用 curl
或 debugging/reconstructing 协议流的浏览器插件。然后第二步是使用 requests
.
示例:当通过 GET 访问您在问题中提到的第一个 URL 时,服务器响应 302 redirection
,其目标在响应的 Location
字段中给出 header。响应还通过 Set-Cookie
header 字段设置一个 cookie。所有这些都很重要。