Python ile Bir haber sitesinden haber başlığı görselleri ve açıklamasını alıp instagramda paylaşacak bir bot yazıyorum bu botu nasıl aktif tutabilirim?
Örnek kod:
import requests
from bs4 import BeautifulSoup
from instabot import Bot
import os
# Sondakika.com'dan haber başlığı ve görselini alacak fonksiyon
def get_news():
url = "https://www.sondakika.com/tuzla/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# Haber başlığı
title = soup.find("div", class_="news-detail").find("h3").text.strip()
# Haber görseli
img_url = soup.find("div", class_="news-detail").find("img")["src"]
img_response = requests.get(img_url)
img_name = "news_image.jpg"
with open(img_name, "wb") as img_file:
img_file.write(img_response.content)
return title, img_name
# Instagram'a gönderi yapacak fonksiyon
def post_to_instagram(title, img_name):
bot = Bot()
bot.login(username="your_username", password="your_password")
caption = f"{title}\n#news #update"
bot.upload_photo(img_name, caption=caption)
os.remove(img_name)
# Ana fonksiyon
def main():
title, img_name = get_news()
post_to_instagram(title, img_name)
if __name__ == "__main__":
main()