I tried to automate the article update of Livedoor blog with Python and selenium.

Hello everyone. This is Nakagawa. This is the first post in a long time. I usually make business web applications using Java at my company, but in general life I often scrape with Python. Especially, I am addicted to automatic blog updates and automatic follow-ups such as sns. So this time, I tried a simple blog update using Python and selenium.

Environment Python 3.7.0 selenium 78

Code

qiita.py


from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

class LivedoorAuto:
	def __init__(self):
		options = webdriver.ChromeOptions()
       #chrome://version See profile path
		options.add_argument("")
       #executable_In path, enter the path of chromedriver
		self.bot = webdriver.Chrome(executable_path="", chrome_options=options)

	def livedoor(self):
		bot = self.bot
        #Blog post page url
		bot.get("")
                
		wait = WebDriverWait(bot,60)
		entry_title = wait.until(expected_conditions.visibility_of_element_located((By.ID,"entry_title")))

		entry_title.send_keys("Hello")

		while True:
			try:
				entry_body = wait.until(expected_conditions.visibility_of_element_located((By.ID,"editor_1_f")))
				entry_body.send_keys("Hello Everyone")
				break
			except TimeoutException:
				print("timeout")
				continue
		
		sleep(2)
		bot.find_element_by_class_name("quickSocialMessage").send_keys("Hello")

	
ed = LivedoorAuto()
ed.livedoor()

It is a code that writes the title hello and the words Hello Everyone in the article content.

When you first access the url, you will be taken to the login page, but if you log in many times, it may be treated as spam, so we try to keep the login information in the session from the beginning. How to keep your login information in selenium is explained in detail on the following page. If you want to keep your site logged in the next time you run Selenium

It's not too difficult, but I didn't have much information like this on the internet, so I wrote it. I hope it will be helpful for you.

Recommended Posts

I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to automate the 100 yen deposit of Rakuten horse racing (python / selenium)
I tried to find the entropy of the image with python
I tried to compare the processing speed with dplyr of R and pandas of Python
I tried to automate the watering of the planter with Raspberry Pi
I tried to make a periodical process with Selenium and Python
I tried to improve the efficiency of daily work with Python
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I tried to automate sushi making with python
I tried to get the authentication code of Qiita API with Python.
I tried to verify and analyze the acceleration of Python by Cython
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to solve the problem with Python Vol.1
I tried to summarize the string operations of Python
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to automate internal operations with Docker, Python and Twitter API + bonus
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
I tried to find the average of the sequence with TensorFlow
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I tried to put out the frequent word ranking of LINE talk with Python
[Python] I tried to visualize the follow relationship of Twitter
Try to automate the operation of network devices with Python
I want to know the features of Python and pip
I tried to enumerate the differences between java and python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
How to write offline real time I tried to solve the problem of F02 with Python
I also tried to imitate the function monad and State monad with a generator in Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to predict the sales of game software with VARISTA by referring to the article of Codexa
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to solve the ant book beginner's edition with python
I want to output the beginning of the next month with Python
I tried to create a list of prime numbers with python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to expand the size of the logical volume with LVM
I tried to easily detect facial landmarks with python and dlib
I tried to automatically collect images of Kanna Hashimoto with Python! !!
PhytoMine-I tried to get the genetic information of plants with Python
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
[Python] I tried to visualize the prize money of "ONE PIECE" over 100 million characters with matplotlib.
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I replaced the numerical calculation of Python with Rust and compared the speed
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried to visualize the age group and rate distribution of Atcoder
I tried to express sadness and joy with the stable marriage problem.
(Python Selenium) I want to check the settings of the download destination of WebDriver