【发布时间】:2020-04-25 10:19:07
【问题描述】:
我知道做一个基于 INSTAGRAM API 的机器人会更好,但我做了一个几个月前可以工作的 selenium firefox,但我知道我想再次运行它,它不再喜欢图片了
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)
# getting hashtags
hrefs_in_view = driver.find_elements_by_tag_name('a')
# finding 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 photos
[pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
except Exception:
continue
# Liking photos
unique_photos = len(pic_hrefs)
for pic_href in pic_hrefs:
driver.get(pic_href)
time.sleep(2)
try:
time.sleep(random.randint(2, 4))
like_button = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button').click()
# liking photo
like_button().click()
time.sleep(random.randint(1, 2))
except Exception as e:
time.sleep(2)
unique_photos -= 1
上面是通过给定标签收集照片然后喜欢它们的代码,收集部分工作正常,但喜欢不会发生。也许这是 XPATH 的问题?这就是我最初的想法,但如果是这种情况,我应该使用哪个?
【问题讨论】:
标签: python selenium selenium-webdriver bots instagram-api