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

HTML标签中用于爬虫识别的额外信息名称及data-vin提取咨询

自定义数据属性介绍与提取方法

一、这类属性的正式名称

你看到的以data-开头的属性是HTML自定义数据属性(Custom Data Attributes),是HTML5标准引入的特性。它允许开发者在HTML元素上附加自定义的私有数据,用于页面脚本逻辑或爬虫提取这类业务相关的额外信息,不会影响元素的正常渲染行为。

二、提取data-vin属性值的方法

以下是几种主流爬虫/解析工具的具体实现示例,基于你提供的HTML片段:

1. Python - BeautifulSoup

from bs4 import BeautifulSoup

# 示例HTML内容
html = '<a href="tel:+1-860-351-9131" id="dealer-phone-e048470c-570f-4a26-841c-b4c258a3298e" class="sds-button--secondary contact-by-phone" phx-hook="CallSourceDniSRPCallButton" phx-update="ignore" data-linkname="dealer-phone-srp" data-activity-rule-type="custom" data-index="1" data-call-intent-id="srp-call-intent" data-listing-id="e048470c-570f-4a26-841c-b4c258a3298e" data-make="Volkswagen" data-model="Taos" data-phone-number="8603519131" data-trim="1.5T SE" data-year="2023" data-vin="3VVNX7B25PM316320">Call</a>'

soup = BeautifulSoup(html, 'html.parser')
# 定位目标a标签(可通过class、id等属性筛选)
target_tag = soup.find('a', class_='contact-by-phone')
# 提取data-vin属性值
vin_value = target_tag.get('data-vin')
print(vin_value)  # 输出: 3VVNX7B25PM316320

2. Python - Scrapy框架

# 在Scrapy爬虫的解析函数中使用
def parse(self, response):
    # CSS选择器方式
    vin_css = response.css('a.contact-by-phone::attr(data-vin)').get()
    # XPath选择器方式
    vin_xpath = response.xpath('//a[@class="sds-button--secondary contact-by-phone"]/@data-vin').get()
    print(vin_css, vin_xpath)  # 均输出目标VIN值

3. Node.js - Cheerio

const cheerio = require('cheerio');

const html = '<a href="tel:+1-860-351-9131" id="dealer-phone-e048470c-570f-4a26-841c-b4c258a3298e" class="sds-button--secondary contact-by-phone" phx-hook="CallSourceDniSRPCallButton" phx-update="ignore" data-linkname="dealer-phone-srp" data-activity-rule-type="custom" data-index="1" data-call-intent-id="srp-call-intent" data-listing-id="e048470c-570f-4a26-841c-b4c258a3298e" data-make="Volkswagen" data-model="Taos" data-phone-number="8603519131" data-trim="1.5T SE" data-year="2023" data-vin="3VVNX7B25PM316320">Call</a>';

const $ = cheerio.load(html);
const vin_value = $('a.contact-by-phone').attr('data-vin');
console.log(vin_value); // 输出: 3VVNX7B25PM316320

4. 原生JavaScript(浏览器环境)

如果是在浏览器控制台或前端脚本中提取:

const targetTag = document.querySelector('a.contact-by-phone');
// 两种提取方式
const vin1 = targetTag.getAttribute('data-vin');
const vin2 = targetTag.dataset.vin; // dataset会自动去除data-前缀,驼峰化属性名
console.log(vin1, vin2); // 均输出目标VIN值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:20:54