如何发送带有空格的单词的get请求

时间:2018-03-30 00:21:56

标签: python python-3.x request

我在下面有这个链接(freepeople + top有+替换空格)

https://poshmark.com/search?query=freepeople+top&type=listings&department=Women

我这样做是为了查询链接:

search='https://poshmark.com/search?'

brand="freepeople"

style="top"
# & seperates parameters
queryParameters={'query':[brand,style],'type':'listings','department':'Women'}
response=requests.get(search,params=queryParameters)

我很困惑,为什么当我打印(response.text)时,它似乎给了我所有的HTML,但是当我这样做时:

MacBook-Air-4:finalproject BCohen$ python3 poshmart.py >/tmp/poshmart.html
MacBook-Air-4:finalproject BCohen$ open /tmp/poshmart.html

它没有带我到一个有效的页面

我假设我可能查询了索引搜索(+)错误,但我不确定如何正确查询它。

1 个答案:

答案 0 :(得分:1)

您可以使用'+'.join([brand,style])将该数组转换为字符串,其中值与+结合使用。结果将是您正在寻找的内容:freepeople+top

import requests

search='https://poshmark.com/search?'

brand="freepeople"
style="top"

print('+'.join([brand,style]))

# & seperates parameters
queryParameters={'query':'+'.join([brand,style]),'type':'listings','department':'Women'}
response=requests.get(search,params=queryParameters)

print(response.request.url)

,输出

freepeople+top
https://poshmark.com/search?query=freepeople%2Btop&type=listings&department=Women

它在第二次打印中显示为%2B的原因是因为这是+

的urlencoded值