声明:本文仅讨论商家主动公开数据的整理与导出,不涉及破解登录、验证码识别、风控对抗、非公开接口调用或隐私数据抓取。请遵守 1688 平台规则、《个人信息保护法》《数据安全法》及相关法律法规,切勿将数据用于骚扰、诈骗或非法营销。本文仅分享实现思路与示例代码。
在电商选品、供应链对接、商务合作等场景中,有时需要整理 1688 商家的公开联系方式。很多商家会在商品详情页、店铺首页或公开资料中展示联系电话。人工复制效率低,容易遗漏,因此可以借助脚本或轻量工具做“公开数据整理”。
本文标题中的“实时采集”,指的是对当前已经公开的页面进行即时解析,而不是绕过平台风控、登录限制或验证码。工具获取的也只是商家主动公开的数据。如果页面没有展示电话,就不应尝试通过非正常手段获取。
一个合规的“1688商家电话导出工具”通常只做以下几件事:
tel: 链接中的电话;需要再次强调:不要使用多线程、代理池高频请求平台,也不要在未获授权的情况下批量抓取。本文示例采用单线程、限速、检查 robots.txt 的方式,仅作技术学习。
安装依赖:
pip install requests beautifulsoup4把你确认可以访问、且页面中已经公开电话的 1688 商品详情页或店铺页 URL 放入代码中的 urls 列表。建议一次只处理少量页面,并在每次请求之间加入延时。
将下方代码保存为 export_1688_phones.py,运行:
python export_1688_phones.py脚本会生成 1688_public_phones.csv,包含“来源URL”和“商家电话”两列。建议导出后人工核对,避免把平台客服电话、无关号码误当成商家电话。
以下代码演示了从公开页面中提取电话并导出 CSV 的基本思路。平台页面结构可能变化,代码不保证对所有页面有效,请勿用于违规批量抓取。
import csv
import re
import time
from urllib.parse import urlparse
from urllib.robotparser import RobotFileParser
import requests
from bs4 import BeautifulSoup
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
)
}
# 匹配手机号、常见座机号
PHONE_PATTERN = re.compile(
r"(?:(?:\+?86)?1[3-9]\d{9}|0\d{2,3}[- ]?\d{7,8})"
)
def can_fetch(url: str) -> bool:
"""检查 robots.txt 是否允许抓取"""
parsed = urlparse(url)
robots_url = f"{parsed.scheme}://{parsed.netloc}/robots.txt"
rp = RobotFileParser()
rp.set_url(robots_url)
try:
rp.read()
except Exception as e:
print(f"读取 robots.txt 失败:{e}")
return False
return rp.can_fetch(HEADERS["User-Agent"], url)
def fetch_html(url: str):
"""获取公开页面 HTML,单线程、限速、超时"""
if not can_fetch(url):
print(f"robots.txt 不允许抓取:{url}")
return None
try:
resp = requests.get(url, headers=HEADERS, timeout=10)
if resp.status_code == 200:
return resp.text
print(f"请求失败,状态码:{resp.status_code},URL:{url}")
except Exception as e:
print(f"请求异常:{e},URL:{url}")
return None
def extract_phones(html: str):
"""从公开 HTML 中提取电话"""
soup = BeautifulSoup(html, "html.parser")
text = soup.get_text(separator=" ", strip=True)
phones = PHONE_PATTERN.findall(text)
# 提取 tel: 链接中的电话
for a in soup.find_all("a", href=True):
href = a["href"].strip()
if href.lower().startswith("tel:"):
phones.append(href[4:].strip())
cleaned = set()
for phone in phones:
phone = re.sub(r"[\s\-()]", "", phone)
if phone.startswith("+86"):
phone = phone[3:]
if phone:
cleaned.add(phone)
return sorted(cleaned)
def export_csv(rows, output_path: str):
"""导出 CSV"""
with open(output_path, "w", newline="", encoding="utf-8-sig") as f:
writer = csv.writer(f)
writer.writerow(["来源URL", "商家电话"])
writer.writerows(rows)
def main():
# 请替换为公开且允许访问的页面 URL
urls = [
# "https://detail.1688.com/offer/xxxxxxxx.html",
]
all_rows = []
for url in urls:
print(f"正在处理:{url}")
html = fetch_html(url)
if not html:
continue
phones = extract_phones(html)
for phone in phones:
all_rows.append([url, phone])
# 单线程限速,避免对平台造成压力
time.sleep(5)
export_csv(all_rows, "1688_public_phones.csv")
print(f"导出完成,共 {len(all_rows)} 条记录。")
if __name__ == "__main__":
main()如果你只是做本地整理,也可以把公开页面手动保存为 HTML,然后用 BeautifulSoup 读取本地文件。这种方式更安全,也更容易控制频率:
from bs4 import BeautifulSoup
import re
def extract_from_local(file_path):
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
html = f.read()
soup = BeautifulSoup(html, "html.parser")
text = soup.get_text(" ", strip=True)
phones = re.findall(r"(?:(?:\+?86)?1[3-9]\d{9}|0\d{2,3}[- ]?\d{7,8})", text)
cleaned = set()
for p in phones:
p = re.sub(r"[\s\-()]", "", p)
if p.startswith("+86"):
p = p[3:]
cleaned.add(p)
return sorted(cleaned)
print(extract_from_local("public_page.html"))robots.txt 不是唯一合规标准。本文分享的“1688商家实时采集软件 / 阿里巴巴卖家电话导出工具”实现思路,核心是:读取公开页面 → 解析公开电话 → 去重 → 导出 CSV。它适合小规模、合规的数据整理场景,不能也不应被用于非法抓取或骚扰营销。
技术本身是中性的,关键在于使用方式。希望这篇教程能帮助你在合规前提下提升整理效率。平台页面结构会变化,代码也可能需要根据实际情况调整。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。