Python 5 - Build Twitter Bot With Webdriver

By Sheldon L Published at 2020-03-15 Updated at 2020-03-15


TwitterBot

cd $hub/mysite_test/python_course/twitter-bot
code .

python3 -m venv venv

export all_proxy=""
pip install pysocks
export all_proxy="socks5://127.0.0.1:1080"

pip install selenium
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.Firefox()

  def login(self):

    """ Open twitter login page """
    bot = self.bot
    bot.get('https://twitter.com/')
    time.sleep(5)
    # comment all following commands and try commands `ed = TwitterBot('account', 'psword')` `ed.login()` and run.
    # grab element in inputbox, see outerHTML, like followings:

    # <input autocapitalize="none" autocomplete="on" autocorrect="off" name="session[username_or_email]" spellcheck="false" type="text" dir="auto" data-focusable="true">
    email = bot.find_element_by_name("session[username_or_email]")
    # <input autocapitalize="none" autocomplete="on" autocorrect="off" name="session[password]" spellcheck="false" type="password" dir="auto" data-focusable="true">
    password = bot.find_element_by_name("session[password]")

    """ Fill in username and password box """
    email.send_keys(self.username)
    password.send_keys(self.password)
    # email.clear()
    # password.clear()

    """ Click Login """
    password.send_keys(Keys.RETURN)
    time.sleep(5)
    # when logged in, search a keywork, inspect how the url looks like

  def like_tweet(self, hashtag):
    bot = self.bot
    # 1. https://twitter.com/hashtag/webdevelopment?src=hashtag_click
    # 2. https://twitter.com/search?q=webdevelopment&src=typed_query
    url = 'https://twitter.com/hashtag/{}?src=hashtag_click'
    bot.get(url.format(hashtag))
    time.sleep(5)
    # when got results, inspect where's the link get into a result, here:
    # <a title="下午12:05 · 2020年3月15日" href="/CouponOnlineCo3/status/1239039871366434816" dir="auto" aria-label="23分钟 前" role="link" data-focusable="true" class="css-4rbku5 css-18t94o4 css-901oao r-1re7ezh r-1loqt21 r-1q142lx r-1qd0xha r-a023e6 r-16dba41 r-ad9z0x r-bcqeeo r-3s2u2q r-qvutc0"><time datetime="2020-03-15T04:05:00.000Z">23分钟</time></a>

    # JavaScript
    for i in range(1, 3):
      bot.execute_script('window.scrollTo(0, document.body.scrollHeight)')
      time.sleep(2)

      tweets = bot.find_elements_by_css_selector('a[dir="auto"]')
      # links = [element.get_attribute('href') for element in tweets]
      hrefs = []
      for element in tweets:
        if 'status' in element.get_attribute('href'):
          hrefs.append(element.get_attribute('href'))

    # try to click `like`
    # <div aria-haspopup="false" aria-label="喜欢" role="button" data-focusable="true" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-11cpok1 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="like"><div dir="ltr" class="css-901oao r-1awozwy r-1re7ezh r-6koalj r-1qd0xha r-a023e6 r-16dba41 r-1h0z5md r-ad9z0x r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0"><div class="css-1dbjc4n r-xoduu5"><div class="css-1dbjc4n r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div><svg viewBox="0 0 24 24" class="r-4qtqp9 r-yyyyoo r-50lct3 r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1srniue"><g><path d="M12 21.638h-.014C9.403 21.59 1.95 14.856 1.95 8.478c0-3.064 2.525-5.754 5.403-5.754 2.29 0 3.83 1.58 4.646 2.73.814-1.148 2.354-2.73 4.645-2.73 2.88 0 5.404 2.69 5.404 5.755 0 6.376-7.454 13.11-10.037 13.157H12zM7.354 4.225c-2.08 0-3.903 1.988-3.903 4.255 0 5.74 7.034 11.596 8.55 11.658 1.518-.062 8.55-5.917 8.55-11.658 0-2.267-1.823-4.255-3.903-4.255-2.528 0-3.94 2.936-3.952 2.965-.23.562-1.156.562-1.387 0-.014-.03-1.425-2.965-3.954-2.965z"></path></g></svg></div></div></div>
    # <div class="css-1dbjc4n r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
    # /html/body/div/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/section/div/div/div/div[1]/div/article/div/div/div[3]/div[4]/div[3]/div/div/div/div
    for href in hrefs:
      bot.get(href)
      buttons = bot.find_elements_by_css_selector('div[role="button"]')
      print(buttons)
      time.sleep(5)

      # try:
      #   bot.find_elements_by_xpath(
      #     '/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div/div/div/section/div/div/div/div[1]/div/article/div/div/div[3]/div[5]/div[3]/div'
      #   ).click()
      #   print('liked')
      #   time.sleep(30)
      # except Exception as ex:
      #   print('not found')
      #   time.sleep(30)

ed = TwitterBot('xiadanli0320@gmail.com', 'atpx4869')

ed.login()
ed.like_tweet('webdevelopment')