{"id":527,"date":"2020-10-11T19:24:11","date_gmt":"2020-10-11T10:24:11","guid":{"rendered":"https:\/\/deskplate.net\/blog\/2020\/10\/11\/python-selenium-%e3%81%a7twitter-%e6%8a%95%e7%a8%bf\/"},"modified":"2020-10-11T19:24:11","modified_gmt":"2020-10-11T10:24:11","slug":"python-selenium-%e3%81%a7twitter-%e6%8a%95%e7%a8%bf","status":"publish","type":"post","link":"https:\/\/deskplate.net\/blog\/archives\/527","title":{"rendered":"Python Selenium \u3067Twitter \u6295\u7a3f"},"content":{"rendered":"<p># dnf install google-chrome<br \/>\n=========================================================================================================================<br \/>\n\u30d1\u30c3\u30b1\u30fc\u30b8                          \u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u30fc    \u30d0\u30fc\u30b8\u30e7\u30f3                     \u30ea\u30dd\u30b8\u30c8\u30ea\u30fc               \u30b5\u30a4\u30ba<br \/>\n=========================================================================================================================<br \/>\n\u4f9d\u5b58\u95a2\u4fc2\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u4e2d:<br \/>\ngoogle-chrome-unstable              x86_64              87.0.4280.11-1                 google-chrome               72 M<br \/>\n# pip install chromedriver-binary==87.0.4280.11-1<br \/>\nWARNING: Running pip install with root privileges is generally not a good idea. Try `pip install &#8211;user` instead.<br \/>\nCollecting chromedriver-binary==87.0.4280.11-1<br \/>\nCould not find a version that satisfies the requirement chromedriver-binary==87.0.4280.11-1 (from versions: 2.29.1, 2.31.1, 2.33.1, 2.34.0, 2.35.0, 2.35.1, 2.36.0, 2.37.0, 2.38.0, 2.39.0, 2.40.1, 2.41.0, 2.42.0, 2.43.0, 2.44.0, 2.45.0, 2.46.0, 70.0.3538.16.0, 70.0.3538.67.0, 70.0.3538.97.0, 71.0.3578.30.0, 71.0.3578.33.0, 71.0.3578.80.0, 71.0.3578.137.0, 72.0.3626.7.0, 72.0.3626.69.0, 73.0.3683.20.0, 73.0.3683.68.0, 74.0.3729.6.0, 75.0.3770.8.0, 75.0.3770.90.0, 75.0.3770.140.0, 76.0.3809.12.0, 76.0.3809.25.0, 76.0.3809.68.0, 76.0.3809.126.0, 77.0.3865.10.0, 77.0.3865.40.0, 78.0.3904.11.0, 78.0.3904.70.0, 78.0.3904.105.0, 79.0.3945.16.0, 79.0.3945.36.0, 80.0.3987.16.0, 80.0.3987.106.0, 81.0.4044.20.0, 81.0.4044.69.0, 81.0.4044.138.0, 83.0.4103.14.0, 83.0.4103.39.0, 84.0.4147.30.0, 85.0.4183.38.0, 85.0.4183.83.0, 85.0.4183.87.0, 86.0.4240.22.0)<br \/>\nNo matching distribution found for chromedriver-binary==87.0.4280.11-1<br \/>\n\u6700\u3082\u8fd1\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<br \/>\n# pip install chromedriver-binary==86.0.4240.22.0<br \/>\nCollecting chromedriver-binary==86.0.4240.22.0<br \/>\nDownloading https:\/\/files.pythonhosted.org\/packages\/08\/85\/4a12e3ea067901e76a68e5a63cd9237b4959a4558a22594fcfcd47ebf648\/chromedriver-binary-86.0.4240.22.0.tar.gz<br \/>\n\/usr\/local\/lib\/python3.6\/site-packages\/setuptools\/distutils_patch.py:26: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools&#8217; objects directly or at least import Setuptools first.<br \/>\n&#8220;Distutils was imported before Setuptools. This usage is discouraged &#8221;<br \/>\nInstalling collected packages: chromedriver-binary<br \/>\nRunning setup.py install for chromedriver-binary &#8230; done<br \/>\nSuccessfully installed chromedriver-binary-86.0.4240.22.0<br \/>\npython \u30b3\u30fc\u30c9<br \/>\n&#8220;`<br \/>\nimport time<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.chrome.options import Options<br \/>\nfrom selenium.webdriver.common.keys import Keys<br \/>\nimport chromedriver_binary<br \/>\n# \u30a2\u30ab\u30a6\u30f3\u30c8\u60c5\u5831<br \/>\naccount = &#8216;username&#8221;<br \/>\npassword = &#8216;password&#8217;<br \/>\n# \u30c4\u30a4\u30fc\u30c8\u3057\u305f\u3044\u6587\u5b57\u5217<br \/>\ntext = &#8220;\u30c6\u30b9\u30c8\u30c4\u30a4\u30fc\u30c8&#8221;<br \/>\n# Twitter\u30ed\u30b0\u30a4\u30f3\u5b9f\u884c\u3059\u308b\u51e6\u7406<br \/>\ndef login_twitter():<br \/>\n# \u30ed\u30b0\u30a4\u30f3\u30da\u30fc\u30b8\u3092\u958b\u304f<br \/>\ndriver.get(&#8216;https:\/\/twitter.com\/login\/&#8217;)<br \/>\ntime.sleep(2)  # \u5f85\u3061<br \/>\n# account\u5165\u529b<br \/>\nelement_account = driver.find_element_by_name(&#8220;session[username_or_email]&#8221;)<br \/>\nelement_account.send_keys(account)<br \/>\ntime.sleep(2)  # \u5f85\u3061<br \/>\n# \u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3059\u308b<br \/>\nelement_pass = driver.find_element_by_name(&#8220;session[password]&#8221;)<br \/>\nelement_pass.send_keys(password)<br \/>\ntime.sleep(3)  # \u52d5\u4f5c\u6b62\u3081\u308b<br \/>\n# \u30ed\u30b0\u30a4\u30f3\u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af<br \/>\nelement_login = driver.find_element_by_xpath(&#8216;\/\/*[@data-testid=&#8221;LoginForm_Login_Button&#8221;]&#8217;)<br \/>\ndriver.execute_script(&#8220;window.scrollTo(0, document.body.scrollHeight);&#8221;)<br \/>\nelement_login.click()<br \/>\ntime.sleep(3) # \u5f85\u3061<br \/>\ndef send_tweet(text):<br \/>\nelement_text = driver.find_element_by_class_name(&#8220;notranslate&#8221;)<br \/>\nelement_text.click()<br \/>\nelement_text.send_keys(text)<br \/>\ntweet_button = driver.find_element_by_xpath(&#8216;\/\/*[@data-testid=&#8221;tweetButtonInline&#8221;]&#8217;)<br \/>\ntweet_button.click()<br \/>\n# selenium\u3092\u8d77\u52d5<br \/>\noptions=Options()<br \/>\noptions.add_argument(&#8216;&#8211;headless&#8217;)<br \/>\ndriver=webdriver.Chrome(chrome_options = options)<br \/>\n# \u30ed\u30b0\u30a4\u30f3\u51e6\u7406<br \/>\nlogin_twitter()<br \/>\n# \u30c4\u30a4\u30fc\u30c8\u3059\u308b<br \/>\nsend_tweet(text)<br \/>\ntime.sleep(10)<br \/>\n# selenium\u3092\u7d42\u4e86<br \/>\ndriver.close()<br \/>\ndriver.quit()<br \/>\n&#8220;`<\/p>\n","protected":false},"excerpt":{"rendered":"<p># dnf install google<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":0,"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"wp:attachment":[{"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deskplate.net\/blog\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}