Web Content Scraper with SQL Integration

Job ID: 40534176

Budget: ₹12,500 – ₹37,500 INR

',
'user': 'your_username',
'password': 'your_password',
'database': 'web_content_db'
}

# 2. ஸ்கிராப் செய்ய வேண்டிய இணையதளங்களின் பட்டியல்
URLS_TO_CRAWL = [
"https://example.com",
"https://example.com",
# மற்ற URL-களை இங்கே சேர்க்கவும்
]

def connect_db():
"""டேட்டாபேஸ் இணைப்பை உருவாக்குகிறது"""
return mysql.connector.connect(**DB_CONFIG)

def clean_text(text):
"""தேவையற்ற இடைவெளிகள் மற்றும் விசித்திரமான எழுத்துக்களை நீக்குகிறது"""
if not text:
return ""
# புதிய வரிகள் மற்றும் இடைவெளிகளை சீரமைத்தல்
cleaned = " ".join(text.split())
return cleaned

def parse_and_insert():
conn = connect_db()
cursor = conn.cursor()

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}

bulk_data = []

for url in URLS_TO_CRAWL:
try:
print(f"Crawling: {url}")
response = requests.get(url, headers=headers, timeout=10)

if response.status_code != 200:
print(f"Skipping {url}: Status code {response.status_code}")
continue

soup = BeautifulSoup(response.content, 'html.parser')

# குறிப்பிட்ட HTML கூறுகளை மட்டும் எடுத்தல் (Headings, Paragraphs, Custom tags)
content_pieces = []

# h1, h2, p மற்றும் உங்களின் பிரத்யேக Tag-களை இங்கே குறிப்பிடவும்
for element in soup.find_all(['h1', 'h2', 'p', 'custom-tag']):
text_val = clean_text(element.get_text())
if text_val:
content_pieces.append(text_val)

# முழு உரையையும் ஒன்றாக இணைத்தல்
full_content = "\n".join(content_pieces)

if full_content:
bulk_data.append((url, full_content, datetime.now()))

# Throttling: இணையதள ஹோஸ்டுக்கு பாதிப்பு ஏற்படாமல் இருக்க 2 வினாடிகள் இடைவெளி
time.sleep(2)

except Exception as e:
print(f"Error crawling {url}: {str(e)}")
continue

# 3. Bulk Insert (ஒரே முறையில் அனைத்து தரவுகளையும் டேட்டாபேஸில் செலுத்துதல்)
if bulk_data:
insert_query = """
INSERT INTO web_pages (source_url, extracted_text, last_updated)
VALUES (%s, %s, %s)
ON DUPLICATE KEY UPDATE
extracted_text = VALUES(extracted_text),
last_updated = VALUES(last_updated);
"""
try:
cursor.executemany(insert_query, bulk_data)
conn.commit()
print(f"Successfully inserted/updated {len(bulk_data)} records.")
except Exception as e:
print(f"Database insert error: {str(e)}")
conn.rollback()

cursor.close()
conn.close()

if __name__ == "__main__":
parse_and_insert()