You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用BeautifulSoup获取服务的关联分类及完整描述字段?

Bulkfollows服务爬取:修复分类与完整描述获取问题

问题背景

需要爬取https://bulkfollows.com/services的服务数据,目标字段包括ID、Service、Rate per 1000、Min / Max、Refill、Avg. Time、Description、category。当前存在两个问题:

  • category列返回None,无法正确关联服务的父分类(如"YouTube - Watch Time By Length")
  • 无法获取点击服务后弹出的完整描述内容

解决方案

以下是修正后的代码,可解决上述两个问题:

from bs4 import BeautifulSoup
import pandas as pd
import requests

url = "https://bulkfollows.com/services"
response = requests.get(url)
soup = BeautifulSoup(response.content, "lxml")

# 构建分类ID与名称的映射
categories = {}
for btn in soup.select('button[data-filter-category-id][data-filter-category-name]'):
    cat_id = btn.get('data-filter-category-id')
    cat_name = btn.get('data-filter-category-name')
    categories[cat_id] = cat_name

# 预抓取所有服务的完整描述(从模态框中提取)
full_descriptions = {}
for modal in soup.select('div[id^="serviceModal-"]'):
    service_id = modal.get('id').replace('serviceModal-', '')
    desc_content = modal.select_one('.service-description').get_text(strip=True, separator='\n')
    full_descriptions[service_id] = desc_content

data = []
for row in soup.select("#serviceList tr:has(td)"):
    # 匹配表头与行数据
    headers = [h.strip() for h in row.find_previous('thead').stripped_strings]
    row_values = [val.strip() for val in row.stripped_strings]
    service_dict = dict(zip(headers, row_values))
    
    # 填充分类信息
    category_id = row.get('data-filter-table-category-id')
    service_dict['category'] = categories.get(category_id, 'Unknown Category')
    
    # 替换为完整描述
    service_id = service_dict.get('ID')
    service_dict['Description'] = full_descriptions.get(service_id, service_dict.get('Description', ''))
    
    data.append(service_dict)

# 生成结果DataFrame
result_df = pd.DataFrame(data)[['ID', 'Service', 'Rate per 1000', 'Min / Max', 'Refill', 'Avg. Time', 'Description', 'category']]
print(result_df)

关键修复点

  1. 分类映射修正:遍历页面所有带有data-filter-category-id和data-filter-category-name属性的按钮,构建准确的分类ID-名称映射,确保服务行的data-filter-table-category-id能匹配到正确分类。
  2. 完整描述获取:提前解析所有服务模态框(ID以serviceModal-开头),提取其中的.service-description内容,通过服务ID关联到对应行,替换原有的简短描述。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 17:45:25