AI网络爬虫:用kimichat自动批量提取网页内容

首先,在网页中按下F12键,查看定位网页元素:

然后在kimi中输入提示词:

你是一个Python编程专家,要完成一个爬取网页内容的Python脚本,具体步骤如下:

在F盘新建一个Excel文件:提示词.xlsx

打开网页:https://lobehub.com/zh/assistants

定位class="layoutkit-flexbox css-15l7r2q acss-vjqh32"的div标签;

定位div标签里面所有的a标签,

提取a标签的href属性值,前面加上"https://lobehub.com/zh",构造成一个URL;

解析这个URL的源代码;

在源代码中定位class="acss-1ce01rv"的h1标签,提取其文本内容作为提示词标题,写入"提示词.xlsx"这个Excel文件的第1列,列的标头为:提示词标题;

在源代码中定位class="layoutkit-flexbox css-o3n4io acss-pl6lf1"的h2标签,提取其文本内容作为提示词简介,写入"提示词.xlsx"这个Excel文件的第2列,列的标头为:提示词简介;

在源代码中定位class="acss-7ksih7"的div标签,提取其全部文本内容作为提示词内容,写入"提示词.xlsx"这个Excel文件的第3列,列的标头为:提示词内容;

注意:

每一步都要输出相关信息到屏幕;

网站有放爬虫机制,要通过设置请求头、延迟请求等方式来进行应对

源代码:

import requests

from bs4 import BeautifulSoup

import pandas as pd

from openpyxl import Workbook

定义目标URL

url = "https://lobehub.com/zh/assistants"

设置请求头,模拟浏览器访问

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'

}

发送请求并获取网页内容

response = requests.get(url, headers=headers)

response.raise_for_status() # 如果请求失败,将抛出异常

解析网页内容

soup = BeautifulSoup(response.text, 'html.parser')

定位div标签并提取所有a标签的href属性

div_tag = soup.find('div', class_='layoutkit-flexbox css-15l7r2q acss-vjqh32')

a_tags = div_tag.find_all('a')

构造完整的URL

urls = "[https://lobehub.com/zh](https://link.zhihu.com/?target=https%3A//lobehub.com/zh "https://lobehub.com/zh")" + tag.get('href') for tag in a_tags

定义Excel文件路径

excel_path = 'F:/提示词.xlsx'

创建工作簿和工作表

wb = Workbook()

ws = wb.active

设置列的标头

ws.append('提示词标题', '提示词简介', '提示词内容')

循环处理每个URL

for idx, url in enumerate(urls, start=1):

print(f"正在处理URL: {url}")

发送请求并获取网页内容

response = requests.get(url, headers=headers)

response.raise_for_status()

解析网页内容

soup = BeautifulSoup(response.text, 'html.parser')

提取h1标签文本内容

h1_tag = soup.find('h1', class_='acss-1ce01rv')

title = h1_tag.get_text(strip=True) if h1_tag else '无标题'

提取h2标签文本内容

h2_tag = soup.find('h2', class_='layoutkit-flexbox css-o3n4io acss-pl6lf1')

introduction = h2_tag.get_text(strip=True) if h2_tag else '无简介'

提取div标签文本内容

div_tag = soup.find('div', class_='acss-7ksih7')

content = div_tag.get_text(strip=True) if div_tag else '无内容'

将提取的内容写入Excel文件

ws.append(title, introduction, content)

打印到屏幕

print(f"标题: {title}")

print(f"简介: {introduction}")

print(f"内容: {content}")

保存Excel文件

wb.save(excel_path)

print(f"数据已写入Excel文件: {excel_path}")

相关推荐
数据知道8 小时前
斩断 `navigator` 前端:底层重写 UserAgent/Platform/Language 属性描述符
爬虫·数据采集·指纹浏览器·浏览器指纹
深蓝电商API13 小时前
Playwright深入浅出:从入门到企业级项目实战
爬虫·playwright
小白学大数据14 小时前
爬虫性能天花板:asyncio赋能 Aiohttp,并发提速 10 倍
开发语言·爬虫·数据分析
yijianace19 小时前
Python爬虫实战:分页爬取 + 详情页采集 + CSV存储
前端·爬虫·python
yijianace20 小时前
Python爬虫实战:ThreadPoolExecutor多线程采集书籍信息与图片下载
开发语言·爬虫·python
在放️20 小时前
Python 爬虫 · bs4 模块基础
开发语言·爬虫·python
belong_my_offer20 小时前
Python 数据采集完全指南 —— 从零开始掌握网络爬虫与文件读取
开发语言·爬虫·python
深蓝电商API20 小时前
Playwright vs Puppeteer vs Selenium 2026终极对比
爬虫·selenium·puppeteer·playwright
遇事不決洛必達1 天前
【Python基础】GIL 锁是什么及其对爬虫的影响
爬虫·python·线程·进程·gil锁
綝~1 天前
爬虫数据采集工程师岗位面试题
爬虫·面试·请求