Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def like_photo(self, hashtag):
- driver = self.driver
- driver.get("https://www.instagram.com/explore/tags/"+ hashtag +"/")
- time.sleep(2)
- #gathering photos
- pic_hrefs = []
- for i in range (1,7):
- try:
- driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
- time.sleep(2)
- #get tags
- hrefs_in_view = driver.find_elements_by_tag_name('a')
- #finding relevant hrefs
- hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view
- if '.com/p/' in elem.get_attribute('href')]
- # building list of unique photos
- [pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
- # print("Check: pic href length " + str(len(pic_hrefs)))
- except Exeption:
- continue
- #liking photos
- unique_photos = len(pic_hrefs)
- for pic_href in pic_hrefs:
- driver.get(pic_href)
- time.sleep(2)
- driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
- try:
- time.sleep(random.randint(2,4))
- like_button = lambda:driver.find_element_by_xpathh('/html/body/span/section/main/section/div[1]/div[1]/div/article[1]/div[2]/section[1]/span[1]/button').click()
- like_button().click()
- for second in reversed(range(0, random.randint(18, 28))):
- print("#" + hashtag + ': unique photos left: ' + str(unique_photos)
- + " | Sleeping " + str(second))
- time.sleep(1)
- except Exception as e:
- time.sleep(2)
- unique_photos -= 1
- username = "username"
- password = "passw"
- ig = InstagramBot(username, password)
- ig.login()
- hashtags = ['kittens']
- while True:
- try:
- # Choose a random tag from the list of tags
- tag = random.choice(hashtags)
- ig.like_photo(tag)
- except Exception:
- ig.closeBrowser()
- time.sleep(60)
- ig = InstagramBot(username, password)
- ig.login()
Add Comment
Please, Sign In to add comment