2016-09-21 48 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没有任何属性不引用。 –