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

Python循环爬取谷歌商家信息报MissingSchema错误如何解决?

报错原因

  • 变量名使用错误:你定义的商家名称列表变量为bus_name,但循环遍历的是未定义的bus变量,结合报错信息判断你实际代码中大概率是误把单个谷歌搜索URL字符串赋值给了google_urls变量,当你执行for url in google_urls时,遍历字符串会每次取出单个字符,首次取出的就是https的首字母h,传入requests.get时就会触发缺少协议头的报错。
  • 业务逻辑错误:你对商家名称做了URL转码生成的html变量完全没有被使用,没有拼接到谷歌搜索URL中,循环逻辑和你预期的「按商家名搜索爬取信息」的需求完全不匹配。

解决方法

直接在每次循环中用当前遍历到的商家名称拼接生成完整的谷歌搜索URL即可,无需提前定义google_urls列表,修正后的参考代码如下:

import requests
import urllib.parse
from fake_useragent import UserAgent

ua = UserAgent()
bus_name = ['go', 'Copper']
# 固定谷歌搜索前缀
google_search_prefix = 'https://www.google.com/search?q='

for name in bus_name:
    # 转码商家名后拼接完整URL
    encoded_name = urllib.parse.quote_plus(name)
    full_url = google_search_prefix + encoded_name
    response = requests.get(full_url, headers={"User-Agent": ua.random})
    # 后续写你解析地址、电话的逻辑即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 16:39:03