Files
ai_keyword_baidu/README.md
2025-12-08 15:44:38 +08:00

97 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 项目简介
本项目用于自动化抓取百度和有来医生页面内容,包括:
- 百度搜索推荐词、相关搜索静态页面requests+BeautifulSoup
- 有来医生文章标题和“大家还在搜”动态页面Playwright 浏览器自动化)
## 目录结构
```
├── baidu.py # 百度搜索推荐词及相关搜索抓取脚本
├── youlai.py # 有来医生页面内容抓取脚本(静态,仅标题)
├── youlai_ui.py # 有来医生页面内容抓取脚本(动态,标题+大家还在搜)
├── requirements.txt # 依赖包列表(完整环境)
├── baidu_result.txt # 百度抓取结果输出样例
├── youlai_result.txt # 有来医生抓取结果输出样例
├── 1.txt, test.py # 辅助文件
```
## 环境创建
建议使用 conda 创建虚拟环境:
```bash
conda create -n xhs python=3.10
conda activate xhs
pip install -r requirements.txt
```
## Playwright 浏览器驱动安装
首次使用 Playwright 需安装浏览器驱动(包括 Chromium/谷歌浏览器):
```bash
playwright install
```
如需仅安装 Chromium谷歌浏览器内核可执行
```bash
playwright install chromium
```
Playwright 会自动下载并配置所需的浏览器驱动,无需手动下载 ChromeDriver。
如需使用本地已安装的 Chrome 浏览器,可在脚本中指定 executable_path 参数。
## 脚本执行方法
### 1. 百度内容抓取
```bash
python baidu.py
```
根据提示输入关键词,结果保存到 baidu_result.txt。
**输出样例:**
```
大家都在搜:
糖尿病早期症状
怎么判断得了糖尿病
...
相关搜索:
糖尿病早期症状
怎么判断得了糖尿病
...
```
### 2. 有来医生静态页面抓取(仅标题)
```bash
python youlai.py
```
结果保存到 youlai_result.txt。
### 3. 有来医生动态页面抓取(推荐,支持 JS 渲染内容)
```bash
python youlai_ui.py
```
结果保存到 youlai_result.txt。
**输出样例:**
```
标题:
糖尿病的临床表现及治疗方法
大家还在搜:
糖尿治疗好方法
眼睛视力模糊是什么原因
...
```
## 依赖说明
所有依赖已在 requirements.txt 中列出,包含 requests、beautifulsoup4、playwright 及完整环境包。
## 常见问题
- Playwright 抓取不到内容时,请确认已正确安装驱动,并适当增加等待时间。
- requests 抓取不到 JS 渲染内容时,请优先使用 youlai_ui.py。
- 如需自定义页面或元素,请根据实际 class 名调整脚本选择器。
- 依赖包如有缺失,可根据报错补充到 requirements.txt。
---
如有问题可随时反馈。