慎用 不知道为啥被封了两个tw号
import json
import random
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
"""
chrome扩展中找到对应扩展id 然后用对应路径打包扩展程序成 .crx
/Users/apple/Library/Application Support/Google/Chrome/Default/Extensions/fhplcfaddamamojjdgblhjbfneadoold
# chrome_options.add_extension('/Users/apple/Desktop/1.1.37_.crx')
# chrome_options.add_extension('/Users/apple/Desktop/metamaskexten.crx')
# chrome://version/--->找用户信息路径
"""
class TW(object):
def __init__(self, driver):
self.chrome_options = Options()
self.driver = driver if driver else webdriver.Chrome(executable_path='chromedriver',
options=self.chrome_options)
def get_cookies(self, id: int):
with open("cookie%d.json" % id) as f:
return json.loads(f.read())
def set_cookies(self, cookie_id: int):
for cookie in self.get_cookies(cookie_id):
self.driver.add_cookie(
{"name": cookie['name'], "value": cookie['value']})
def retweet(self, url: str = ''):
retweet_status = self.driver.find_element("xpath",
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div[1]/div/section/div/div/div[1]/div/div/div[1]/article/div/div/div/div[3]/div[7]/div/div[2]/div").get_attribute(
"aria-label")
print(retweet_status)
if str(retweet_status) not in ['Retweeted', 'مُعاد تغريدها']:
try:
self.driver.find_element("xpath",
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div[1]/div/section/div/div/div[1]/div/div/div[1]/article/div/div/div/div[3]/div[7]/div/div[2]").click()
self.driver.find_element("xpath",
"//*[@id='layers']/div[2]/div/div/div/div[2]/div/div[3]/div/div/div/div").click()
print('retweet success')
except Exception as e:
print("retweet err", url, e)
def follow(self, url: str):
try:
# WebDriverWait(self.driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "//*[@data-testid='placementTracking']/div/div/div/span/span"))
# )
follow_status = self.driver.find_element("xpath",
"//*[@data-testid='placementTracking']/div/div/div/span/span").text
print(follow_status)
if follow_status not in ['Following', 'متابَع']:
# follow
self.driver.find_element("xpath",
"//*[@data-testid='placementTracking']/div/div/div/span").click()
print("follow success")
except Exception as e:
print("follow err ", url, e)
def like(self, url: str):
try:
like_status = self.driver.find_element("xpath",
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div[1]/div/section/div/div/div[1]/div/div/div[1]/article/div/div/div/div[3]/div[7]/div/div[3]/div").get_attribute(
"aria-label")
print(like_status)
if str(like_status) not in ['Liked', 'تمّ الإعجاب']:
self.driver.find_element("xpath",
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div[1]/div/section/div/div/div[1]/div/div/div[1]/article/div/div/div/div[3]/div[7]/div/div[3]").click()
print('like success')
except Exception as e:
print("like err", url, e)
def reply(self, url: str, reply_data: str):
try:
self.driver.find_element("xpath",
"//*[@id='react-root']/div/div/div[2]/main/div/div/div/div[1]/div/section/div/div/div[1]/div/div/div[1]/article/div/div/div/div[3]/div[7]/div/div[1]").click()
self.driver.find_element("xpath",
"//*[@id='layers']/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div/div[3]/div/div[2]/div/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div/label/div[1]/div/div/div/div/div/div[2]/div").send_keys(
reply_data)
time.sleep(1)
self.driver.find_element("xpath",
"//*[@id='layers']/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div/div[3]/div/div[2]/div/div/div/div/div[2]/div[3]/div/div/div[2]").click()
time.sleep(0.5)
print('reply success')
except Exception as e:
print("reply err", url, e)
def pretreatment(self):
try:
if self.driver.find_element("xpath","/html/body/div[2]/div/form/input[5]").get_attribute("value") == "Continue to Twitter":
self.driver.find_element("xpath", "/html/body/div[2]/div/form/input[5]").click()
print('pretreatment success')
time.sleep(1)
except Exception as e:
pass
def run(url: str, cookie_id: int, url_type: int, reply_data: str = "give a like"):
chrome_options = Options()
prefs = {
'profile.default_content_setting_values': {
'images': 2,
# 'permissions.default.stylesheet': 2,
# 'javascript': 2
}
}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path='chromedriver', options=chrome_options)
tw = TW(driver)
tw.driver.maximize_window()
tw.driver.get(url)
tw.set_cookies(cookie_id)
tw.driver.get(url)
time.sleep(4)
tw.pretreatment()
if url_type == 1:
tw.follow(url)
if url_type == 2:
tw.like(url)
tw.retweet(url)
tw.reply(url, reply_data)
if __name__ == '__main__':
data = {
1: ['https://twitter.com/BitCloutCat',
],
2: [
'https://twitter.com/BitCloutCat/status/1600075467016802304',
]
}
for i in range(4, 15):
if i not in [9,11]:
print("cookie----------------------->:", i)
for k, v in data.items():
for url in v:
run(url, cookie_id=i, url_type=k, reply_data=random.choice(
["Hey look, it's already got a 'like'.",
'is cool',
"Please don't forget to hit the like button",
"666",
"Great article and plenty of information too. Good artilcle."]))