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

Python爬虫仅抓取谷歌搜索前10条结果问题求助

问题原因及解决办法

为什么只能抓取前10条结果里的邮箱?

  • 谷歌搜索采用动态懒加载机制:默认只在页面初始化时渲染前10条搜索结果,剩余结果需要用户滚动页面到底部、触发加载动作后才会逐步加载到页面DOM中。
  • 你的代码在打开页面后立刻获取了driver.page_source,此时页面仅加载了前10条结果的HTML内容,自然只能提取到这部分里的邮箱。
  • 代码里的time.sleep(10)放在了获取页面源之后,完全起不到等待加载更多结果的作用。

解决办法:滚动页面加载全部结果

你需要在获取页面源之前,模拟用户滚动页面的动作,把所有100条结果都加载出来。以下是修改后的代码:

from selenium import webdriver
import time
import re
import pandas as pd

PATH = 'C:\Program Files (x86)\chromedriver.exe'

target_url = "https://www.google.com/search?q=solicitors+wales+%27email%27+%40&rlz=1C1CHBD_en-GBIT1013IT1013&sxsrf=AJOqlzWC1oRbVtWcmcIgC4-3ZnGkQ8sP_A%3A1675764565222&ei=VSPiY6WeDYyXrwStyaTwAQ&ved=0ahUKEwjlnIy9lYP9AhWMy4sKHa0kCR4Q4dUDCA8&uact=5&oq=solicitors+wales+%27email%27+%40&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzIFCAAQogQyBwgAEB4QogQyBQgAEKIESgQIQRgASgQIRhgAUABYAGD4AmgAcAF4AIABc4gBc5IBAzAuMZgBAKABAcABAQ&sclient=gws-wiz-serp"

driver = webdriver.Chrome(PATH)
driver.get(target_url)
time.sleep(2)  # 先等初始页面加载完成

# 模拟滚动页面加载所有结果
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
    # 滚动到页面底部
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    # 等待加载新内容
    time.sleep(3)
    # 计算新的页面高度,对比之前的高度
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        # 如果高度不变,说明没有更多内容可以加载了
        break
    last_height = new_height

# 现在获取完整的页面源,提取邮箱
email_pattern = r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,4}"
html = driver.page_source
emails = re.findall(email_pattern, html)

# 去重(避免重复的邮箱)
unique_emails = list(set(emails))

# 保存到Excel
df = pd.DataFrame(unique_emails, columns=['Email Addresses'])
df.to_excel('email_addresses_.xlsx', index=False)

driver.close()

额外注意事项

  • 谷歌有反爬机制,频繁滚动或抓取可能会触发验证码,建议适当延长等待时间,避免过快操作。
  • 部分搜索结果里的邮箱可能不会直接显示在页面源中(比如通过JS动态生成),这种情况可能需要点击进入对应页面再抓取,但这会增加被反爬的风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 21:20:33