2017-10-11 8 views
2

PARAMS行方不明 - 標準、jupyterのノートPCのIDEをインストールのpython3 get.requests URLは私が窓10上のpython 3.6を実行している

コード:

import requests 

params={'q': 'NASDAQ:AAPL','expd': 19, 'expm': 1, 'expy': 2018, 'output': 'json'} 
response = requests.get('https://www.google.com/finance/option_chain', params=params) 

print(response.url) 

予想される出力:

https://finance.google.com/finance/option_chain?q=NASDAQ:AAPL&expd=19&expm=1&expy=2018&output=json 

を実際の出力:

https://finance.google.com/finance/option_chain?q=NASDAQ:AAPL&output=json 

私のコードを見ていただきありがとうございます!

-E

答えて

0

CONCAT文字列を使用して、私の回避策です。 HTTPステータスコードは302で、新しいURLにリダイレクトされました。

あなたはここでの詳細情報を取得することができます https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirectionshttp://docs.python-requests.org/en/master/user/quickstart/#redirection-and-history

は、我々はリダイレクトを追跡するために、Responseオブジェクトの履歴プロパティを使用することができます。この方法を試してください。

import requests 

params={'q': 'NASDAQ:AAPL','expd': 19, 'expm': 1, 'expy': 2018, 'output': 
'json'} 
response = requests.get('https://www.google.com/finance/option_chain', 
params=params) 
print(response.history) # use the history property of the Response object to track redirection. 
print(response.history[0].url) # print the redirect history's url 
print(response.url) 

あなたが取得します:

[<Response [302]>] 
https://www.google.com/finance/option_chain?q=NASDAQ%3AAAPL&expm=1&output=json&expy=2018&expd=19 
https://finance.google.com/finance/option_chain?q=NASDAQ:AAPL&output=json 

をあなたはallow_redirectsパラメータで取り扱うリダイレクトを無効にすることができます

import requests 

params={'q': 'NASDAQ:AAPL','expd': 19, 'expm': 1, 'expy': 2018, 'output': 'json'} 
response = requests.get('https://www.google.com/finance/option_chain', params=params, allow_redirects=False) 
print(response.status_code) 
print(response.url) 
0

これは本当に答えではないですが、URLのリダイレクトを取得しているので、それは

response = requests.get(url, params=params) 
    response_url = response.url 
    added_param = False 
    for i in params: 
     if response_url.find(i)==-1: 
      added_param = True 
      response_url = response_url+"&"+str(i)+"="+str(params[i]) 
      print("added:",str(i)+"="+str(params[i]), str(response_url)) 
    if added_param: 
     response = requests.get(response_url) 
関連する問題