2016-09-21 41 views
0

我有蟒蛇下面的代碼:如何使用python請求獲取particaular鏈接?

import requests 
#specific gtrends for Mcdonalds 
#the referred output is https://www.google.com/trends/explore?gprop=news&q=%2Fm%2F07gyp7 
search_params = {'gprop' : "news", 'q' : "%2Fm%2F07gyp7" } 
gtrend_resp = requests.get("https://www.google.com/trends/explore", params = search_params) 
print(gtrend_resp.url) 

我怎麼能包括%2的網址是什麼?

回答

1

引文結束首先:

>>> import urllib 
>>> urllib.unquote("%2Fm%2F07gyp7") 
'/m/07gyp7' 

接着,下面的代碼:

search_params = {'gprop' : "news", 'q' : "/m/07gyp7" } 
gtrend_resp = requests.get("https://www.google.com/trends/explore", params = search_params) 
print(gtrend_resp.url) 

產生所需網址:

https://www.google.com/trends/explore?gprop=news&q=%2Fm%2F07gyp7 
+0

謝謝!它的工作,但出於某些原因。第一次嘗試給了我這個結果https://www.google.com/trends/explore?q=%2Fm%2F07gyp7&gprop=news雖然我有同樣的順序 –

+1

@pythonnovice當然,你的意思是查詢參數的排序是不同的?這是好的和預期的,排序實際上不被保留,它應該不重要。 – alecxe

+0

哦,不知道。非常感謝你。所以只要鍵和相應的值在他們各自的匹配。我使用Python 3.5,它得到,urllib沒有任何屬性不引用。 –