[PYTHON] Automatic browser operation that even beginners can do

I think the biggest hurdle for programming beginners is building an environment. This time, the environment construction is as small as possible, And I chose a language with few unclear descriptions. But in the end it got longer. This time, the chrome will move automatically.

What to download this time

(I don't install the big software that seems to be written in the beginner's book of common programs)

--Editor ** Visual Studio Code (hereinafter VS Code) ** --Language Python --Tools required for automatic browser operation chromedriver.exe(selenium)

Put out the letters for the time being

VS Code installation

First, download an editor called Visual Studio Code. It is an easy-to-use editor that beginners do not want to throw out because there are too many items like IDE (super high-performance editor such as eclipse, netbeans, Visual Studio). There are various other editors like this, but my impression is that atom → startup is slow and annoying, not for windows, sublime, brackets → various expansions by myself are troublesome)

Of course, it is automatically in Japanese. Someone who recommends English tools to programming beginners. Check it to make it easier to work later. (It will be registered in the right-click menu of the file. People who are annoyed with this kind of thing do not have to check it.)

Python installation

Then DL & install Python https://www.python.org/downloads/

Check Add to PATH. (You will be able to type the python command and pip command. If you forget to check it, add it to the environment variable yourself.)

(By the way, the tool used for automatic browser operation this time is selenium, but you can use any language. Probably there are 3 of java C # Python. However, I chose Python because java and C # are often annoying for beginners.)

I also need chromedriver.exe, but I will leave it for the time being

Implementation

For the time being, I will put out the characters. Create a suitable folder. Right click on the folder and click Open with Code Screenshot_17.png

Then right-click on the left menu screen → create a new file with the name "hello.py". Screenshot_20.gif

Once created

print("konnnitiwa seaki")

And save with Ctrl + S </ kbd>. Screenshot_21.png

After saving, press Ctrl + Shift + C </ kbd> in VS Code to open the command prompt. Enter python hello.py. If you say konnnitiwa sekai, it's a success. Screenshot_22_.png

Easy to use VS Code

However, this is no different from just Notepad, so enable error display and automatic input. Install the VS Code python extension If you press CTRL + P </ kbd> in VS Code, this screen will appear. Screenshot_23.png

Type ʻext install Python` and click on the many DL Pythons below about 5 to install. Screenshot_18.png

During the installation, the bottom left is round and round. Screenshot_24_.png

Reboot when you're done. Then, I think that the following message appeared above. (It will appear if you play with the file appropriately and save it) Screenshot_25.png I'm told to put in a code checker called pylint. Open a command prompt with Ctrl + Shift + C </ kbd> Install pylint with pip install pylint. Screenshot_26.png This will check your code for errors when you save it. (No need to restart VS Code) Screenshot_27.png By the way, when you execute it one by one, it is tedious to open the command prompt and enter it, so the program execution is automated with a shortcut. (Please wait a little longer for automatic browser operation) Also, press Ctrl + P </ kbd> to display this screen. Type > tasks config and click Configure Task Liner → Click Others. Screenshot_28.png Screenshot_29.png

A configuration folder called .vscode will be created, and a configuration file called tasks.json will be created. Change it as follows. (You only need to change the command and args parts.) Screenshot_30.png

Now you can go back to the hello.py screen and press Ctrl + Shift + B </ kbd> to run the program in VS Code without having to type a command. sekai.gif

(It is a configuration file that only executes the command python file name with Ctrl + Shift + B </ kbd>) (By the way, vscode, Japanese characters in the console are garbled for the solid compliment. If you execute the file from the command prompt, Japanese characters will not be garbled. ) Screenshot_32.png

Browser automatic operation

Preparation

It's finally browser operation. DL chromedriver.exe. https://sites.google.com/a/chromium.org/chromedriver/downloads Screenshot_14_.png Screenshot_16_.png

Unzip and place chromedriver.exe in the same folder as hello.py. Screenshot_33_.png

Also open a command prompt pip install selenium Type. You will be able to use selenium in python. Tools for browser operation require this selenium and chromedriver.exe.

Implementation

Copy the following code from hello.py.

hello.py


from selenium import webdriver #Use the selenium webdriver that you installed with pip install selenium earlier

driver = webdriver.Chrome("./chromedriver.exe") #The chrome driver that I just downloaded.use exe
driver.get("http://www.yahoo.co.jp/") #Start chrome and move to yahoo
searchBox = driver.find_element_by_css_selector("#srchtxt") #Search for html in the search input box
searchBox.send_keys("I hate the world") #その検索ボックスで 「I hate the world」と打つ
kensakuBotan = driver.find_element_by_css_selector("#srchbtn") #Find the search button in html
kensakuBotan.click() #Click the search button

Run

Save with Ctrl + S </ kbd> and then run with Ctrl + Shift + B </ kbd> Then, chrome will start up and search without permission. Screenshot_22.png Did you do it? Now you and you are scripters from today Find the contents of find_element_by_css_selector () by actually opening yahoo in your browser and looking at the html source. With chrome, you can use developer tools with F12, and you can get a css selector for html parts like this. chrome.gif

Digression

pip is like an app store for python. You can download various things that can be used with python with the pip install ○○ command. What you installed with pip install selenium Maybe in C: \ Users \ username \ AppData \ Local \ Programs \ Python \ Python35-32 Maybe the library gets messed up so I usually create a virtual environment, but I didn't do it because it's annoying. I'm new to selenium so I don't know if scripters in the world are using it. Also, I have never written python.

Recommended Posts

Automatic browser operation that even beginners can do
Python-PageRank calculation & social graph visualization that even monkeys can do
8 services that even beginners can learn Python (from beginners to advanced users)
IOS application development with Python (kivy) that even monkeys can do
Variable naming anti-patterns that beginners tend to do
"First time Kikaigakushu" that even university students with no knowledge can do --AWS SageMaker
An introduction to Word2Vec that even cats can understand
Even beginners can do it! An easy way to write a Sankey Diagram on Plotly