如何使用python请求获取特定链接?

时间:2016-09-21 01:36:12

标签: python-3.x url python-requests

我在python中有以下代码:

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 个答案:

答案 0 :(得分: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