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}")

相关推荐
深蓝电商API17 小时前
mitmproxy高级用法:自定义脚本拦截改包
爬虫·mitmproxy
Fluxproxy18 小时前
Python爬虫异步提速终极方案|线程池ThreadPoolExecutor实战,告别单线程慢速爬取
开发语言·爬虫·python
独立开发者阿乐18 小时前
llms.txt 是什么?为什么 AI 爬虫需要这个文件
人工智能·爬虫·geo优化·ai爬虫·llms.txt·ai收录·robots.txt
傻啦嘿哟21 小时前
高校信息爬虫:爬取全国高校列表与排名,做择校分析
爬虫
深蓝电商API1 天前
HTTP/2 协议逆向:从 Fiddler 抓不到包说起
爬虫·网络协议·http·fiddler
隐擎fox2 天前
深入网络风控底层:解密 IP 信誉评分模型、ASN 属性判定与多租户原生网络隔离架构
网络·爬虫·网络协议·tcp/ip·架构·跨境电商·指纹浏览器
zzp282182 天前
Estyapp逆向分析
爬虫
天启HTTP2 天前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
SEO_juper3 天前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
阿狗童鞋3 天前
Python爬虫进阶实战指南
开发语言·爬虫·python