python - 我正在制作一个机器人,它喜欢所有尚未被喜欢的帖子
问题描述
问题是它不喜欢这些帖子。
我尝试过标签名称等差异方法
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
def like_photo(self):
driver = self.driver
driver.get("https://www.instagram.com")
time.sleep(1)
for i in range(1, 4):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
# find all the heart links
hrefs = driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
pic_hrefs = [href for href in pic_hrefs]
print(' Photos ' + str(len(pic_hrefs)))
for _ in pic_hrefs:
driver.get("https://www.instagram.com")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
like_button = lambda: driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
like_button.click()
time.sleep(18)
except Exception as e:
time.sleep(1)
nameIG = InstagramBot(username, password)
nameIG.login()
nameIG.like_photo()
它像任何帖子一样,输出只是:照片4
进程以退出代码 0 结束
解决方案
exit code 0
意味着您的代码正在运行且没有错误。但是,仍然存在问题。
要查看代码中是否存在实际错误,请更改异常操作。
except Exception as e:
print(e) # shows actual error
尝试这个:
like_buttons = driver.find_elements_by_xpath(some_xpath_to_buttons) # list of WebElements
for button in like_buttons:
button.click()
time.sleep(18)
推荐阅读
- azure - 我可以使用 Azure IoT 边缘网关在叶设备和 Azure IoT 中心之间进行双向通信吗?
- node.js - 无法从 Node 中的 Model.findByID 获取数据 - Express
- c++ - 为什么在函数之外指针数据没有得到更新
- c++ - CMake为目标设置自定义配置名称
- python-3.x - 没有值的 for 循环的输出文件
- elasticsearch - Elasticsearch - 正确的反向边缘 ngram 标记器
- python-3.x - 将 Celery 与烧瓶路线一起使用
- c++ - 设置停靠和选项卡式 CDockablePane 的框架颜色
- javascript - Django/CKEditor/Javascript 问题,包括 textarea 中的文本
- ruby-on-rails - 如何在 Elasticsearch 中编写“或”查询?