Robotslxml.txt #5108
Wichitrawinu
started this conversation in
Ideas
Robotslxml.txt
#5108
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
pip install -r requirements.txt
python main_bot.py
my_crawler/
├── main_bot.py # โค้ดหลักที่เราเขียนกัน
├── crawler_cache.db # ไฟล์ฐานข้อมูล (จะเกิดขึ้นเอง)
├── requirements.txt # ไฟล์รายการ Library
└── README.md # คู่มือการใช้งาน
import smtplib
import schedule
import time
from email.message import EmailMessage
class AutomatedCrawler(UltimateCrawler):
def init(self, email_config):
super().init()
self.email_config = email_config
--- การตั้งค่าและเริ่มการทำงาน ---
CONFIG = {
'sender_email': 'your_email@gmail.com',
'password': 'your_app_password', # ใช้ App Password ของ Google
'receiver_email': 'target_email@gmail.com'
}
def job():
print(f"⏰ เริ่มรอบการทำงาน ณ เวลา {time.ctime()}")
bot = AutomatedCrawler(CONFIG)
--- ตั้งเวลา (Scheduler) ---
ทำงานทุกวันเวลา 08:30 น.
schedule.every().day.at("08:30").do(job)
หรือจะให้ทำทุกๆ 1 ชั่วโมงก็ได้:
schedule.every(1).hours.do(job)
print("🔋 ระบบ Scheduler เริ่มทำงานแล้ว... (กด Ctrl+C เพื่อหยุด)")
while True:
schedule.run_pending()
time.sleep(60) # ตรวจสอบตารางเวลาทุก 1 นาที
def solve_captcha(site_key, page_url):
api_key = "YOUR_2CAPTCHA_API_KEY"
# ส่งคำขอไปที่ Service
post_url = f"http://2captcha.com/in.php?key={api_key}&method=userrecaptcha&googlekey={site_key}&pageurl={page_url}"
response = requests.get(post_url).text
requests==2.31.0
lxml==5.1.0
beautifulsoup4==4.12.3
schedule==1.2.1
import requests
from lxml import etree
from bs4 import BeautifulSoup
import time
from urllib.parse import urljoin
class UltimateCrawler(SmartCrawler): # สืบทอดความสามารถ SQLite มาจากโค้ดก่อนหน้า
def init(self, user_agent="MyBot/1.0"):
super().init()
self.headers = {"User-Agent": user_agent}
--- การใช้งานจริง ---
crawler = UltimateCrawler()
1. ขุดหารูปแบบ URL ทั้งหมดจาก Sitemap หลัก
target_sitemap = "https://www.example.com/sitemap_index.xml"
target_urls = crawler.get_all_urls_recursive(target_sitemap)
for url in target_urls:
if not crawler.is_visited(url):
print(f"🚀 กำลังดึงข้อมูลจาก: {url}")
import requests
import re
import time
from urllib.parse import urljoin
class ProfessionalRobotsChecker:
def init(self, user_agent_name="*"):
self.target_agent = user_agent_name.lower()
self.rules = []
self.sitemaps = []
self.crawl_delay = 0 # วินาที
--- วิธีการนำไปใช้ในโปรเจ็กต์ ---
bot = ProfessionalRobotsChecker(user_agent_name="MySuperBot")
print(bot.fetch_robots("https://www.google.com"))
1. แสดงรายการ Sitemap ที่เจอ
print(f"📂 Sitemaps found: {bot.sitemaps}")
2. เช็ค Crawl-delay
print(f"⏱️ Crawl-delay: {bot.crawl_delay} seconds")
3. จำลองการทำงานแบบสุภาพ (Polite Crawling)
target_path = "/search/how-to-code"
if bot.can_fetch(target_path):
bot.wait_before_crawl() # รอถ้าเว็บสั่งให้รอ
print(f"🚀 เริ่มดึงข้อมูลจาก {target_path}...")
else:
print(f"🚫 ถูกปฏิเสธการเข้าถึง {target_path}")
import requests
import sqlite3
import time
from lxml import etree
from urllib.parse import urljoin
class SmartCrawler:
def init(self, db_name="crawler_cache.db"):
# 1. ตั้งค่าฐานข้อมูล SQLite
self.conn = sqlite3.connect(db_name)
self._init_db()
self.sitemaps = []
--- วิธีการนำมาประกอบร่าง (Integration Example) ---
สมมติว่าเราได้ sitemaps มาจาก ProfessionalRobotsChecker (โค้ดก่อนหน้า)
my_crawler = SmartCrawler()
found_urls = my_crawler.parse_sitemap("https://www.example.com/sitemap.xml")
for url in found_urls:
# 1. เช็คว่าเคยไปหรือยัง (SQLite)
if my_crawler.is_visited(url):
print(f"⏭️ ข้าม {url} (เคยไปแล้ว)")
continue
import csv
import json
class UltimateCrawler(SmartCrawler):
# ... (โค้ดเดิมจากขั้นตอนก่อนหน้า) ...
--- ตัวอย่างการรันกระบวนการทั้งหมด ---
crawler = UltimateCrawler()
all_scraped_results = []
สมมติว่านี่คือลูปการทำงานของบอท
urls = ["https://example.com/p1", "https://example.com/p2"]
for url in urls:
# 1. Scrape ข้อมูล
data = crawler.scrape_data(url)
# เพิ่ม URL เข้าไปในชุดข้อมูลด้วยเพื่อให้รู้ว่ามาจากไหน
data['url'] = url
all_scraped_results.append(data)
3. ส่งออกข้อมูล (Export)
crawler.save_to_csv(all_scraped_results)
crawler.save_to_json(all_scraped_results)
Beta Was this translation helpful? Give feedback.
All reactions