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

2024-09-30 00:38:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面有这个链接(freepome+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)

我不明白为什么我打印的时候(回复.text)它似乎给了我所有的html,但当我这样做:

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

它不会把我带到一个有效的页面

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


Tags: httpscomsearch链接tophtmltypequery
1条回答
网友
1楼 · 发布于 2024-09-30 00:38:08

可以使用'+'.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值

相关问题 更多 >

    热门问题