Python ve BeautifulSoup4 kütüphanesini kullanarak basit bir web scraping projesi denedim. Sizce nasıl? Kodda iyileştirilebilecek yerler var mı?
Program çalıştırıldıktan sonra sadece akşam 22:00'a kadar veri almak üzere kodlandı.
Not: Program sadece eğitim amaçlıdır, kötü bir amacım yoktur. Program sitede yoğunluk yapmasın diye veriyi her saat başı çekmesi için ayarladım ve sürekli olarak kullanmadım, sadece testler için çalıştırdım ve sürekli açık tutmadım. Böyle bir kodun paylaşımı yasaksa moderatörler konuyu onaylamazsa mutlu olurum.
Program çalıştırıldıktan sonra sadece akşam 22:00'a kadar veri almak üzere kodlandı.
Python:
import os
import requests
from bs4 import BeautifulSoup
import schedule
import time
from datetime import datetime, timedelta
current_dir = os.path.dirname(os.path.abspath(__file__))
csv_file_path = os.path.join(current_dir, "online_members_data.csv")
def save_data(member_count, guest_count, total_count):
with open(csv_file_path, "a") as file:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file.write(f"{now},{member_count},{guest_count},{total_count}\n")
def fetch_data():
url = "https://techolay.net/sosyal/"
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, "html.parser")
online_info = soup.find("span", class_="block-footer-counter")
if online_info:
text = online_info.get_text()
total, member, guest = parse_online_info(text)
save_data(member, guest, total)
else:
print("Çevrimiçi üye sayısı bilgisi bulunamadı.")
else:
print(f"Veri alınamadı. {response.status_code}")
def parse_online_info(text):
total = int(text.split("Toplam:")[1].split("(")[0].strip())
member = int(text.split("üye:")[1].split(",")[0].strip())
guest = int(text.split("misafir:")[1].split(")")[0].strip())
return total, member, guest
def schedule_jobs():
fetch_data()
now = datetime.now()
next_hour = (now + timedelta(hours=1)).replace(minute=0, second=0, microsecond=0)
next_run_time = next_hour.strftime("%H:%M")
schedule.every().day.at(next_run_time).do(fetch_data)
for hour in range(next_hour.hour + 1, 22):
schedule.every().day.at(f"{hour:02d}:00").do(fetch_data)
if __name__ == "__main__":
if not os.path.exists(csv_file_path):
with open(csv_file_path, "w") as file:
file.write("timestamp,member_count,guest_count,total_count\n")
schedule_jobs()
while True:
schedule.run_pending()
time.sleep(1)
Not: Program sadece eğitim amaçlıdır, kötü bir amacım yoktur. Program sitede yoğunluk yapmasın diye veriyi her saat başı çekmesi için ayarladım ve sürekli olarak kullanmadım, sadece testler için çalıştırdım ve sürekli açık tutmadım. Böyle bir kodun paylaşımı yasaksa moderatörler konuyu onaylamazsa mutlu olurum.
Son düzenleyen: Moderatör: