[英]Sending get request using python-requests
Sending get request with query string but requests
is stripping the query string. 发送带有查询字符串的get请求,但是
requests
正在剥离查询字符串。 I tried encoding it with urllib.parse.urlencode()
but still the result is same with requests
. 我试图用编码它
urllib.parse.urlencode()
但仍是结果相同与requests
。 Any help please... 任何帮助请...
import requests
url = 'https://www.booking.com/searchresults.html'
params = {
'checkin_monthday': '19',
'checkin_year': '2018',
'checkout_month': '4',
'checkout_monthday': '20',
'checkout_year': '2018',
'class_interval': 1,
'dest_id': -1022488,
'dest_type': 'city',
'dtdisc': 0,
'from_sf': 1,
'group_adults': 2,
'group_children': 0,
'inac': 0,
'index_postcard': 0,
'label': 'gen1',
'label_click': 'undef',
'no_rooms': 1,
'offset': 0,
'postcard': 0,
'raw_dest_type': 'city',
'room1': 'A,A',
'sb_price_type': 'total',
'sb_travel_purpose': 'business',
'src': 'index',
'src_elem': 'sb',
'ss': 'Pokhara',
'ss_all': 0,
'ssb': 'empty',
'sshis': 0,
'ssne': 'Pokhara',
'ssne_untouched': 'Pokhara',
}
# import urllib
# formatted_query_string = urllib.parse.urlencode(payload)
# url = url + '?' + formatted_query_string
r = requests.get(url, params=params)
print(r.url)
# output
# https://www.booking.com/searchresults.html?dest_id=-1022488;est_type=city;ss=Pokhara
Your code is fine, no need to use urllib
. 您的代码很好,不需要使用
urllib
。 The reason why you are getting "stripped" URL is that this is not the initial URL you're looking for. 之所以获得“剥离” URL,是因为这不是您要查找的初始 URL。
If you examine requests
source code , you can find that r.url
is 如果检查
requests
源代码 ,则可以发现r.url
是
Final URL location of Response
So r.url
is not the URL you requested, this is URL you have been (finally) redirected to. 所以
r.url
不是您请求的URL,这是您(最终)重定向到的URL。 You can make a simple test: 您可以进行一个简单的测试:
from requests import Request, Session
url = 'https://www.booking.com/searchresults.html' # your url
params = { # I intentionally shortened this dict for testing purposes
'checkin_monthday': '19',
'checkin_year': '2018',
'checkout_monthday': '20',
'checkout_year': '2018',
}
req = Request('GET', url, params=params)
prepped = req.prepare()
print(prepped.url) # you send this URL ...
s = Session()
resp = s.send(prepped)
print(resp.url) # ... but you are redirected to this URL (same as your r.url)
Output: 输出:
https://www.booking.com/searchresults.html?checkin_year=2018&checkin_monthday=19&checkout_monthday=20&checkout_year=2018
https://www.booking.com/
What happens here: 这里会发生什么:
HTTP/1.1 301 Moved Permanently
. HTTP/1.1 301 Moved Permanently
作出响应。 Redirect location can be found in headers: Location: https://www.booking.com/searchresults.html?dest_id=-1022488;est_type=city;ss=Pokhara
. Location: https://www.booking.com/searchresults.html?dest_id=-1022488;est_type=city;ss=Pokhara
;est_type= Location: https://www.booking.com/searchresults.html?dest_id=-1022488;est_type=city;ss=Pokhara
。 requests
receives this response and goes to this location. requests
会收到此响应,然后转到该位置。 The location URL is put to r.url
attribute. r.url
属性。 That's why initial URL and final URL are different. 这就是为什么初始URL和最终URL不同的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.