首页 > 教程攻略 > ai资讯 >AI爬取网页表格内容保存为excel

AI爬取网页表格内容保存为excel

来源:互联网 时间:2026-06-09 14:41:50

你可能会遇到这样一个场景:盯着网页上一大堆结构化表格数据,心想——“这些要是能一键导入Excel,该有多好?” 别急,这事儿靠AI就能搞定。这里就以一个真实案例来演示:从 Amplify Partners 的投资组合页面,把每一个被投公司的名称、所在地、状态、链接、团队成员、里程碑和文章等信息,统统爬下来存进一个Excel文件。

AI爬取网页表格内容保存为excel

开始之前,需要给AI写一段提示词。目标非常清晰:用Python写一个爬虫脚本,完成以下任务——

  • 在F盘新建一个 Excel 文件,命名为 Amplifypartnersportfolio.xlsx
  • 打开 https://www.amplifypartners.com/portfolio
  • 定位所有 role="listitem"div 元素,逐个提取其中包含的以下信息:
    • class 为 mob-namediv 文本 → 写入 Excel 第1列(Name);
    • class 为 copy-wrdivp 标签文本 → 写入第2列(Location);
    • 属性 fs-cmsfilter-field="status"div 文本 → 写入第3列(Status);
    • class 为 mob-linkdiv 文本 → 写入第4列(Link);
    • 第1个 class="flex-vertical gap-4"div 中所有 p 标签文本,过滤掉“TEAM”和空字符串 → 写入第5列(Team);
    • 第2个同类 div 中所有 p 文本,过滤掉“Milestones”和空字符串 → 写入第6列(Milestones);
    • 第3个同类 div 中所有 p 文本,过滤掉“Articles”和空字符串 → 写入第7列(Articles);
  • 每一步的处理结果都要输出到屏幕,方便跟踪和调试。

下面是 Deepseek 据此生成的 Python 代码,核心逻辑清晰,直接可用:

import requests
from bs4 import BeautifulSoup
import openpyxl

workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = "Amplify Partners Portfolio"

sheet['A1'] = 'Name'
sheet['B1'] = 'Location'
sheet['C1'] = 'Status'
sheet['D1'] = 'Link'
sheet['E1'] = 'Team'
sheet['F1'] = 'Milestones'
sheet['G1'] = 'Articles'

url = 'https://www.amplifypartners.com/portfolio'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

list_items = soup.find_all('div', role='listitem')
row = 2

for item in list_items:
    try:
        name_div = item.find('div', class_='mob-name')
        name = name_div.text.strip() if name_div else ''
        print(f"Extracted Name: {name}")
        sheet[f'A{row}'] = name

        location_div = item.find('div', class_='copy-wr')
        location = location_div.find('p').text.strip() if location_div and location_div.find('p') else ''
        print(f"Extracted Location: {location}")
        sheet[f'B{row}'] = location

        status_div = item.find('div', {'fs-cmsfilter-field': 'status'})
        status = status_div.text.strip() if status_div else ''
        print(f"Extracted Status: {status}")
        sheet[f'C{row}'] = status

        link_div = item.find('div', class_='mob-link')
        link = link_div.text.strip() if link_div else ''
        print(f"Extracted Link: {link}")
        sheet[f'D{row}'] = link

        team_div = item.find('div', class_='flex-vertical gap-4')
        if team_div:
            team_members = [p.text.strip() for p in team_div.find_all('p') if p.text.strip() not in ['TEAM', '']]
            team_str = ', '.join(team_members)
            print(f"Extracted Team: {team_str}")
            sheet[f'E{row}'] = team_str

        milestones_div = item.find_all('div', class_='flex-vertical gap-4')[1] if len(item.find_all('div', class_='flex-vertical gap-4')) > 1 else None
        if milestones_div:
            milestones = [p.text.strip() for p in milestones_div.find_all('p') if p.text.strip() not in ['Milestones', '']]
            milestones_str = ', '.join(milestones)
            print(f"Extracted Milestones: {milestones_str}")
            sheet[f'F{row}'] = milestones_str

        articles_div = item.find_all('div', class_='flex-vertical gap-4')[2] if len(item.find_all('div', class_='flex-vertical gap-4')) > 2 else None
        if articles_div:
            articles = [p.text.strip() for p in articles_div.find_all('p') if p.text.strip() not in ['Articles', '']]
            articles_str = ', '.join(articles)
            print(f"Extracted Articles: {articles_str}")
            sheet[f'G{row}'] = articles_str

        row += 1
    except Exception as e:
        print(f"Error processing item: {e}")

workbook.sa ve('F:/Amplifypartnersportfolio.xlsx')
print("Data sa ved to Amplifypartnersportfolio.xlsx")

这个脚本的逻辑并不复杂,但覆盖了从网页抓取到Excel输出的完整链路:

  • 骨架搭建

    :用 openpyxl 创建Excel工作簿并设置表头;
  • 铁锹

    :用 requests 拿下页面内容,BeautifulSoup 解析HTML;
  • 精雕细琢

    :通过 findfind_all 精准定位每一个数据槽;
  • 入库

    :将提取的数据填入对应的Excel列;
  • 收工

    :把工作簿保存到指定路径。

运行脚本后,控制台会逐条打印提取结果,方便你实时确认每一步都走对了。几分钟后,你就会在F盘根目录下看到 Amplifypartnersportfolio.xlsx,点开之后——所有数据整整齐齐地躺在表格里,分毫不差。

相关下载