← Writing
Automation

Lead Generation Automation ด้วย n8n

ดึงข้อมูลจาก Keyword ด้วย n8n มาเก็บไว้ใน Google Sheets

17 Aug 202514 min readChatGPTAI AgentDashboard

Lead Generation Automation ด้วย n8n

ดึงข้อมูลจาก Keyword ด้วย n8n มาเก็บไว้ใน Google Sheets

Connect with me and follow our journey: Linkedin, Facebook

สวัสดีครับ! ยินดีต้อนรับกลับมาสู่ซีรีส์ AI Automation ที่ผมทำขึ้นเพื่อแบ่งปันความรู้ในการสร้างระบบอัตโนมัติให้ทำงานแทนเราได้จริง ในตอนที่ผ่านมาเราได้เรียนรู้ตั้งแต่การติดตั้ง n8n, การสร้าง AI Agent, ไปจนถึงการเชื่อมต่อ Google Services และสร้าง Mini Project สนุก ๆ กันไปแล้ว

ใน EP.5 นี้ เราจะก้าวไปอีกขั้นกับหัวข้อ Lead Generation Automation หรือการสร้างระบบเพื่อค้นหาและเก็บข้อมูลลูกค้าเป้าหมาย (เช่น ชื่อ, อีเมล, เบอร์โทรศัพท์) โดยอัตโนมัติ

Lead Generation คืออะไร?

Lead Generation คือกระบวนการค้นหาและรวบรวมข้อมูลติดต่อ (เช่น ชื่อ, เบอร์โทร, อีเมล) ของกลุ่มลูกค้าเป้าหมายที่เราสนใจ เพื่อนำไปใช้ต่อยอดด้านการตลาด การขาย หรือการสร้างฐานลูกค้าใหม่

ในบทความนี้ ผมจะสาธิตการสร้าง Workflow ด้วย n8n (Localhost) โดยใช้ 3 องค์ประกอบหลัก:

  1. Google Custom Search API → ใช้ค้นหากลุ่มเป้าหมายจาก Facebook หรือทั้งเว็บ
  2. HTTP Request → ดึงข้อมูลจาก URL ที่ค้นเจอ
  3. Google Sheets → บันทึกผลลัพธ์อัตโนมัติ

Demo 1: Facebook

  • Load File ที่ชื่อ lead-fb-gen ใน VS Code

  • เปิด Terminal
    - พิม npm install ซึ่งเป็นคำสั่งที่ใช้สำหรับติดตั้ง dependencies ที่ระบุไว้ในไฟล์ package.json ของโปรเจกต์ Node.js (ถ้าเจอ Error อะไรลองถาม ChatGPT แล้วทำตามดู)

  • ต่อด้วยคำสั่ง node sc แล้วกด Tab (ของผมจะขึ้นเป็น node .\scrape-fb-contact.js กด Enter → Server ก็จะ Run ขึ้นมา

  • กด Get started → กด เพิ่ม

  • ใส่ข้อมูลต่างๆ
    - ชื่อเครื่องมือค้นหา: fb-lead-gen
    - ป้อน Website หรือหน้า: *.facebook.com/*
    - กด “สร้าง”

  • เลือก “กลับไปที่เครื่องมือทั้งหมด”

  • กด “เพิ่ม”

  • ใส่อีกอัน
    - ชื่อเครื่องมือค้นหา: lead-google
    - สิ่งที่จะค้นหา: เลือก “ค้นหาทั้งเว็บ”
    - กด “สร้าง”

  • ถ้าเลือก “กลับไปที่เครื่องมือทั้งหมด” → จะพบสิ่งที่เราสร้าง 2 อัน

  • กด Enable (ถ้าเจอ Manage คือถูกต้อง)

  • กดที่ Hamberger Icon มุมซ้ายบน → เลือก APIs & Services → เลือก Credentials

  • เลือก + Create credentials → API key → Copy เก็บไว้นะ อย่าลืม

  • Copy รหัสเครื่องมือค้นหา มาเก็บไว้

  • กลับไปอีกอันนึง

  • Copy รหัสเครื่องมือค้นหา มาเก็บไว้

  • ตอนนี้เราจะมีรหัสผ่าน 3 ประกอบด้วย

Google API Key: Axxxo
FB CX: 8xxxb
Google CX: 9xxx7

  • ไปที่ n8n → Add first step → On form submission

  • กด Code ไปอีก 2 node แล้ว add HTTP Request

  • เลือก HTTP Request → กด Expression แล้วกดขยาย

  • ใส่ Code ตามนี้ลงไป

https://www.googleapis.com/customsearch/v1?
key=&cx=&q=site:facebook.com+{{
$('+').item.json['ธุรกิจที่ต้องการ'] }}+{{ $('+').item.json['จังหวัด'] }}&num=10&start={{
$json.start }}

ภาพหลังใส่เสร็จแล้ว

  • add Code ไปอีก 2 node, add Loop และ add HTTP request อีกที

  • เลือก HTTP Request → กด Expression แล้วกดขยาย

  • ใส่ Code ตามนี้ลงไป

http://host.docker.internal:3000/scrape?url={{ $json.cleanedUrl }}

Note: ถ้าทำบน render ต้องแก้ไข http://host.docker.internal:3000 เป็น ngrok ของตัวเอง

  • add “Edit Fields” และ “Google Sheet: Append or update row in sheet”

ถึงเวลาตั้งค่า (configure) nodes ทั้งหมดให้เรียบร้อย

  • Node: On from submission
    - Form Title: Keywords
    - Form Elements
    — Field Name: ธุรกิจที่ต้องการ
    — Element Type: Text
    — Placeholder:เสริมความงาม
    — เปิด Required Field

  • ถ้าต้องการให้กรอกข้อมูลมากกว่า Keywords ก็สามารถเพิ่ม Element ได้ โดยกด Add Form Element

  • กด Execute step จะแสดง Form ให้กรอกดังรูป

  • ลองกรอกข้อมูลดู (ผมลองกรอก เสริมความงาม, ระยอง, 10) → ลองดู Output ที่ Node แรก

  • ต่อไปเราจะอธิบายการทำงานของ Code แต่ละ Node กัน

  • Code ใน node ที่ 2

for (const item of $input.all()) {
const original = item.json['ธุรกิจที่ต้องการ'] || '';

// แทนที่ช่องว่างทั้งหมดด้วย +
const replaced = original.replace(/\s+/g, '+');

// ส่งค่าใหม่กลับไปใน field ใหม่ หรือจะทับ field เดิมก็ได้
item.json.cleanedBusiness = replaced;
}

// ✅ คืนค่าผลลัพธ์ทั้งหมดกลับไป
return $input.all();

ในกรณีที่มีคนกรอกฟอร์ม มามากกว่า 1 Keywork เช่น “เสริมความงาม กวดวิชา” code จะเปลี่ยนให้เป็น “เสริมความงาม+กวดวิชา”

  • Code ใน node ที่ 3

const requestedAmount = parseInt($json["จำนวน"], 10) || 10; // ดึงค่าจากฟอร์ม (default 10)
const resultsPerPage = 10;

const output = [];

for (let i = 1; i <= requestedAmount; i += resultsPerPage) {
output.push({ json: { start: i } });
}

return output;

Node 3 เอาค่าจำนวนที่ผู้ใช้กรอกมา แบ่งเป็นรอบๆ (ทีละ 10) แล้วสร้าง output หลายก้อน โดยแต่ละก้อนบอกว่าจะเริ่มที่ record ลำดับที่เท่าไหร่ เช่นถ้ากรอก 20 รอบแรกจะเริ่มที่ 1 รอบ 2 จะเริ่มที่ 11

  • HTTP Request ใน node ที่ 4

ทำการเชื่อมต่อกับ Google Custom Search ด้วย API และ Search AI ที่เราทำการสร้างไว้

  • Code ใน node ที่ 5

const output = [];

for (const item of $input.all()) {
const items = item.json.items || [];

for (const result of items) {
output.push({
json: {
url: result.link || '',
title: result.title || '',
snippet: result.snippet || ''
}
});
}
}

return output;

เป็นการแปลงรูปแบบข้อมูล เพื่อให้เข้าใจง่ายและพร้อมใช้ต่อ

  • Code ใน node ที่ 6

const output = [];

for (const item of $input.all()) {
const url = item.json.url || '';

// 1) decode %E0%B8... -> ตัวอักษรจริง
let cleaned = decodeURIComponent(url);

// 2) ตัด query string ออก (ทุกอย่างหลัง ?)
cleaned = cleaned.split('?')[0];

output.push({
json: {
originalUrl: url, // เก็บไว้เผื่ออ้างอิง
cleanedUrl: cleaned, // URL ที่ผ่านการ clean แล้ว
title: item.json.title || '',
snippet: item.json.snippet || ''
}
});
}

return output;

เป็นการ Clean URL ให้อ่านง่ายขึ้น

  • Loop ใน node ที่ 7 ทำหน้าที่ส่งสิ่งที่เราอยากค้นหาเข้าไปทีละ 1
  • HTTP Request ใน node ที่ 8 ทำหน้าที่ ส่งข้อมูลที่เราต้องการกลับมา ซึ่งคือ Email กับเบอร์โทร

Note: การ Run Node นี้ต้องเปิด Server 3000 ซึ่งถูกสั่งให้เปิดใน VS code ด้านบนนะ

  • Edit Fields ใน node 9 ทำหน้าที่เลือกข้อมูลที่เราต้องการ เพื่อจะส่งเข้า Google Sheet

  • Google Sheet ใน node 10 เลือกสิ่งที่อยากได้เข้ามาแสดงผล

  • Output ที่ได้

Demo 2: Google

  • copy ทั้งหมดลงมาอีกชุดนึง

  • ใน HTTP Request แรก → กด Expression แล้วกดขยาย

  • เปลี่ยน code ด้านล่างจาก FB CX เป็น Google CX

https://www.googleapis.com/customsearch/v1?
key=&cx=&q=site:facebook.com+{{
$('+').item.json['ธุรกิจที่ต้องการ'] }}+{{ $('+').item.json['จังหวัด'] }}&num=10&start={{
$json.start }}

  • ผลลัพธ์ที่ได้ (หลังปรับแต่งและเพิ่ม Parameter ต่างๆเข้าไป)

Key Takeaways

  • Lead Generation คือหัวใจของการสร้างฐานลูกค้า → การทำให้เป็น Automation ช่วยประหยัดเวลาอย่างมาก
  • Google Custom Search API สามารถกำหนด Scope ได้ว่าจะค้นหาเฉพาะเว็บไหน
  • การเชื่อม n8n + API + Google Sheets ช่วยให้ได้ Workflow ที่ครบตั้งแต่ค้นหาจนเก็บผลลัพธ์

ใน EP.5 นี้ เราได้เรียนรู้การสร้างระบบ Lead Generation Automation ที่สามารถค้นหา Facebook/Google → ดึง Email & เบอร์โทร → เก็บลง Google Sheets ได้ครบจบใน Workflow เดียว เหมาะสำหรับใครที่ต้องการสร้างระบบช่วยทีมขายและการตลาด


Data Science Explore the world of data science with Donato_Story

Dashboard Discover the power of data visualization with Donato_Story

Donato_Journey Join me on my journey (Thai version)

Course_Review Discover the training courses with Donato_Story (Thai version)

Let’s Connect!

Your thoughts and feedback are invaluable. Feel free to share them in the comments or connect with me on

Originally published on Medium

Related