You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python googlesearch库:如何去除后台的%20或+符号以正常使用

解决googlesearch库搜索字符串重复编码问题

你遇到的问题根源是手动对查询字符串做了URL编码(quote(z)),但googlesearch库内部会自动处理URL编码,双重编码导致搜索字符串中出现多余的%20或+符号,破坏了原本的搜索语法,导致无法正常抓取结果。

修复后的代码

from googlesearch import search

query = input("Enter the company name:")
# 直接构造原始查询字符串,无需手动编码
z = '''-intitle:"profiles" -inurl:"dir/" site:ch.linkedin.com/in/ OR site:ch.linkedin.com/pub/ Current: "''' + query + '"'
search_results = list(search(z, num_results=10))

for i, result in enumerate(search_results, start=1):
    print(f"Result{i}:{result}")

说明

googlesearch库的search()函数会自动处理查询字符串中的特殊字符(比如空格、引号)的URL编码,不需要提前用urllib.parse.quote()手动编码。如果手动编码后再传入,会造成重复编码,比如空格被转成%20后,又被再次编码为%2520,或者被重复替换为+,导致谷歌无法解析正确的搜索指令。

内容的提问来源于stack exchange,提问作者John Moores

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 15:25:55