You need to enable JavaScript to run this app. 使用Python Selenium爬取Quora内容的相关技术问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Selenium爬取Quora内容的相关技术问题咨询

Quora爬取问题解决方案

修正后完整可用代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from bs4 import BeautifulSoup

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# 规避反爬检测
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

wd = webdriver.Chrome(options=options)
wd.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

# 这里替换成你要爬取的链接
URL = "https://www.quora.com/What-was-your-prom-like"
# URL = "https://www.quora.com/profile/%E0%AE%AA%E0%AE%BE%E0%AE%B2%E0%AE%BE%E0%AE%9C%E0%AE%BF-%E0%AE%B5%E0%AE%BF%E0%AE%B8%E0%AF%8D%E0%AE%B5%E0%AE%A8%E0%AE%BE%E0%AE%A4%E0%AE%A9%E0%AF%8D-Balaji-Viswanathan"

wd.get(URL)
PAUSE_TIME = 3
# 最大滚动次数,避免死循环
MAX_SCROLL = 20
scroll_count = 0

# --- 自动登录逻辑(可选,需要用的话取消注释,填自己的账号密码)---
# def login_quora(username, password):
#     try:
#         # 点击登录按钮
#         login_btn = WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="登录"]' if '中文' in wd.title else '//button[text()="Log in"]')))
#         login_btn.click()
#         # 输入邮箱
#         email_input = WebDriverWait(wd, 10).until(EC.presence_of_element_located((By.XPATH, '//input[@name="email"]')))
#         email_input.send_keys(username)
#         # 输入密码
#         pass_input = wd.find_element(By.XPATH, '//input[@name="password"]')
#         pass_input.send_keys(password)
#         # 提交登录
#         submit_btn = wd.find_element(By.XPATH, '//button[@type="submit" and (text()="登录" or text()="Log in")]')
#         submit_btn.click()
#         time.sleep(5)
#         print("登录成功")
#     except Exception as e:
#         print("登录失败,可能遇到人机验证,可手动处理或加载已登录的cookie绕过:", str(e))
#
# # 调用登录
# # login_quora("你的邮箱", "你的密码")
# --- 登录逻辑结束 ---

lh = wd.execute_script("return document.body.scrollHeight")

while scroll_count < MAX_SCROLL:
    # 滚动前先展开所有截断的回答
    expand_btns = wd.find_elements(By.XPATH, '//button[contains(text(), "Continue Reading") or contains(text(), "More")]')
    for btn in expand_btns:
        try:
            # 用js点击避免元素不可见报错
            wd.execute_script("arguments[0].click();", btn)
            time.sleep(0.5)
        except:
            continue
    # 滚动到页面底部
    wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(PAUSE_TIME)
    nh = wd.execute_script("return document.body.scrollHeight")
    if nh == lh:
        break
    lh = nh
    scroll_count += 1

# 提取内容
spans = wd.find_elements(By.CSS_SELECTOR, 'span.q-box.qu-userSelect--text')
for span in spans:
    text = span.text.strip()
    if text:
        print(text)
        print('-' * 70 + '\n')

# 提取所有Quora站内链接
quora_links = set()
links = wd.find_elements(By.TAG_NAME, 'a')
for link in links:
    href = link.get_attribute('href')
    if href and href.startswith('https://www.quora.com/'):
        quora_links.add(href)
print("===== 提取到的站内链接 =====")
for l in quora_links:
    print(l)

wd.quit()

问题对应解法说明

  • 第一个问题(个人主页无限死循环):原代码没有设置滚动次数上限,个人主页内容如果出现滚动高度不变但内容加载失败、或者登录弹窗遮挡的情况就会陷入死循环。新增MAX_SCROLL参数限制最大滚动次数,同时增加了反爬规避参数,避免Quora识别到自动化工具限制内容加载,兼容问题页和个人主页两种场景。
  • 第二个问题(回答被截断):每次滚动前先定位页面上所有Continue Reading和More按钮,用JS强制点击展开所有被截断的内容,再提取文本就能拿到完整回答,点击时加异常捕获跳过失效的按钮。
  • 第三个问题(提取站内链接):用Selenium直接获取页面所有a标签的href属性,过滤前缀为https://www.quora.com/的链接即可,不用单独转BeautifulSoup解析,如果你要用BeautifulSoup可以拿wd.page_source传给BeautifulSoup后再筛选,效果一致。原方法无效一般是因为页面还没加载完全就开始提取,或者动态加载的内容没有被BeautifulSoup获取到。
  • 第四个问题(自动登录):代码里已经附上了登录逻辑,取消注释后填入你的Quora账号密码即可调用。如果遇到人机验证,可先在本地Chrome手动登录Quora,导出登录后的cookie,爬取时提前加载cookie,就可以绕过登录验证,不会触发人机检查。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 22:45:00