Geçen haftadan beri sıkılıp küçük küçük scriptler yazmaya başladım ve ortaya koyduğum için mutlu olduğum iki şey var şuan elimde. Biri bu sabah oturup 1-2 saat için yazdığım ve ismini çok yaratıcı şekilde pytagram koyduğum küçük bir scraper. Kısaca username olarak girdi verdiğiniz profildeki fotoğrafların tamamını çekip kaydediyor. Hata kontrolleri yok hala kod içinde ve kodun yorum satılarını bile tamamlamadım ama bilmediğim modülleri kullanarak oluşturmuş olmanın verdiği bir hazla paylaşmak istedim.
Not : pytagram'ı çalıştırmak için chromedriver'ı veya muadili bir yazılımı bilgisayarınıza indirmeniz ve path'ini atamanız gerekiyor. Kodun içindeki chromeları firefox ile değiştirip firefox'un webdriver'ını da kullanabilirsiniz sanırım. Not 2 : *nix sistemlerde denemedim, o yüzden biri deneyip durumu reportlarsa sevinirim.
import urllib from selenium import webdriver from bs4 import BeautifulSoup from time import sleep from os import mkdir, chdir, getcwd
counter = 0 # Just a dummy counter variable. username = input("Please enter a username :") # We are taking user input for username. link = 'https://www.instagram.com/{}/'.format(username)# This is the place where we creating link. pathofchrome = 'C:\chromedriver.exe' # You have to specify where is your chromedriver.exe for launch it. driver = webdriver.Chrome(pathofchrome) # I create a object for launch headless chrome. driver.get(link) # We are navigating to profile url.
mkdir("{}".format(username)) # We are creating a directory for download all the pics in it. chdir("{}".format(username)) # And ofc we are changing our directory.
height = driver.execute_script("return document.body.scrollHeight") # I'm taking length of page for later. driver.find_element_by_class_name("_8imhp").click() # This is the place where we are clicking the "Load more" button.
while True: driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')# This is where we are scrolling down to bottom. sleep(0.8) # Here is a little cooldown for bug. newHeight = driver.execute_script("return document.body.scrollHeight") if newHeight == height: break else: height = newHeight
for x in aList: counter = counter + 1 urllib.request.urlretrieve(x, '{}.jpg'.format(str(counter))) print(str(counter) + " downloaded.")
Yazdığım diğer şey ise geceleri yatmadan bilgisayarı zamanlı komut ile kapatmaya üşendiğim için hem Windows hem Linux sistemlerle çalışacak olan bir shutdown scripti oldu.
from os import name from sys import exit import shutdown
if __name__ == "__main__": while True: try: print("For shutdown : 1\nFor cancel shutdown : 2\nFor exit : 3") choice = int(input("Choice : ")) break except ValueError: print("Please enter a valid value.") if choice == 1: if name == "nt": shutdown.winShut() if name == "posix": shutdown.nixShut() elif choice == 2: shutdown.cancelShut() elif choice == 3: exit()
shutdown.py
from subprocess import call from os import name
def winShut(): while True: try: time = int(input("How many minutes do you want to shut down? : ")) * 60 break except ValueError: print("Please enter an integer.")
sdc = 'shutdown -s -t {}'.format(time) call(sdc)
def nixShut(): while True: try: time = int(input("How many minutes do you want to shut down? : ")) break except ValueError: print("Please enter an integer.")