[英]Sending get request using python-requests
发送带有查询字符串的get请求,但是requests
正在剥离查询字符串。 我试图用编码它urllib.parse.urlencode()
但仍是结果相同与requests
。 任何帮助请...
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
您的代码很好,不需要使用urllib
。 之所以获得“剥离” URL,是因为这不是您要查找的初始 URL。
如果检查requests
源代码 ,则可以发现r.url
是
Final URL location of Response
所以r.url
不是您请求的URL,这是您(最终)重定向到的URL。 您可以进行一个简单的测试:
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)
输出:
https://www.booking.com/searchresults.html?checkin_year=2018&checkin_monthday=19&checkout_monthday=20&checkout_year=2018
https://www.booking.com/
这里会发生什么:
HTTP/1.1 301 Moved Permanently
作出响应。 重定向位置可以在标题中找到: 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
会收到此响应,然后转到该位置。 位置URL放入r.url
属性。 这就是为什么初始URL和最终URL不同的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.