python网页抓取:如何阅读Twitter类名或IDS或 DIV.name...或任何标识符



大家好,我对我创建的以下代码有问题, 当我登录并选择那些div 时,我无法识别类名或 id 或 Twitter 页面的名称 然后对他们所有人点赞,我把旧的类名,即推特

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class TwitterBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.bot = webdriver.Chrome()
def login(self):
bot = self.bot
#bot.get("Url of twitter to /login/")
time.sleep(2)
email = bot.find_elements_by_name('session[username_or_email]')[1]
password = bot.find_elements_by_name('session[password]')[1]
email.clear()
password.clear()
email.send_keys(self.username)
password.send_keys(self.password)
password.send_keys(Keys.RETURN)
time.sleep(1)
def like_tweets(self,Hashtag):
bot = self.bot
bot.get('https://twitter.com/search?q='+ Hashtag +'&src=typd')
time.sleep(1)
for i in range(1,5):
bot.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(1)
tweets = bot.find_elements_by_class_name("tweet")
links  = [elem.get_attribute('data-permalink-path') for elem in tweets]
for link in links:
bot.get('https://twitter.com' + link)
try:
#print(links)
bot.find_elements_by_class_name('HeartAnimation').click()
time.sleep(10)
except Exception as ex:
time.sleep(60)

Omar = TwitterBot('My username', 'My Password')
Omar.login()
Omar.like_tweets('مرتضي منصور')  

最新的推特以某种方式设法阻止了硒的这种选择。你可以试试pyautogui。我的建议是专注于推特官方API,因为我也失败了。这种类型的机器人也违反了推特的条款。干杯!

最新更新