我正在尝试用 selenium 制作一个 twitter 机器人,它将获取推文的链接,然后一一回复。YouTube上有很多教程。我有很多问题。首先,只有当我退出我的推特账户时,推文才会在弹出窗口中打开,否则不会。我不知道原因。当推文在弹出窗口中打开时,心形按钮的类是“HeartAnimation”。如果我登录了,我无法获得推文的“数据-pemalink-path”。现在我找不到回复按钮的类别。我正在使用的课程不起作用。它正在抛出 NoSuchElement 异常。这是我的代码。如果我做错了什么,请告诉我。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
hashtag = 'pycon'
driver.get('https://twitter.com/search?q='+hashtag+'&src=typd')
time.sleep(3)
for i in range(1,3):
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(30)
tweets = driver.find_elements_by_class_name('tweet')
links = [elem.get_attribute('data-permalink-path')
for elem in tweets]
time.sleep(3)
driver.get('https://twitter.com/login?prefetchTimestamp=1569146832519')
username_field = driver.find_element_by_class_name('js-username-field.email-input.js-initial-focus')
username_field.send_keys('email')
time.sleep(0.5)
pass_field = driver.find_element_by_class_name('js-password-field')
pass_field.send_keys('password')
time.sleep(0.5)
driver.find_element_by_class_name('submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium').click()
time.sleep(3)
for link in links:
driver.get('https://twitter.com' + link)
time.sleep(7)
driver.find_element_by_class_name('css-1dbjc4n.r-18u37iz.r-1h0z5mdr-3qxfft.r-h4g966.r-rjfia').click()
time.sleep(2)
reply = driver.find_element_by_class_name('notranslate.public-DraftEditor-content')
reply.send_keys('wow!')
time.sleep(2)
driver.find_element_by_class_name('css-18t94o4.css-1dbjc4n.r-urgr8i.r-42olwf.r-sdzlij.r-1phboty.r-rs99b7.r-1w2pmg.r-1n0xq6e.r-1vuscfd.r-1dhvaqw.r-1fneopy.r-o7ynqc.r-6416eg.r-lrvibr').click()
time.sleep(8)
ibeautiful
慕仙森
相关分类