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

求助:ImportError无法从selenium导入start_chrome

问题解决步骤

错误原因分析

  1. 脚本文件名冲突:你的脚本命名为helium.py,这会和第三方库helium重名,导致Python导入时优先加载当前目录的脚本,而非真正的helium库,引发循环导入问题。
  2. 导入路径错误:start_chrome是helium库封装的方法,并非selenium原生提供,所以从selenium导入必然报错。

修正后的代码

首先将脚本文件名修改为非库名的名称(比如quote_scraper.py),然后使用以下代码:

from helium import *
from bs4 import BeautifulSoup

start_url = 'http://quotes.toscrape.com/'

# 直接使用helium提供的start_chrome方法
driver = start_chrome(url=start_url, headless=True)

soup = BeautifulSoup(driver.page_source, 'html.parser')

quotes = soup.find_all('div', {'class':'quote'})

for item in quotes:
    # 提取纯文本而非标签对象
    print(item.find('span', {'class':'text'}).get_text())

关键修改说明

  • 重命名脚本文件:彻底解决库名冲突问题,确保Python能正确导入第三方helium库。
  • 移除错误导入:删除from selenium import start_chrome,helium已经封装了启动Chrome的逻辑,无需从selenium单独导入。
  • 优化输出:使用get_text()提取引号的纯文本内容,避免打印HTML标签对象。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 16:00:57