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.
(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)
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.)
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
For the time being, I will put out the characters. Create a suitable folder. Right click on the folder and click Open with Code
Then right-click on the left menu screen → create a new file with the name "hello.py".
Once created
print("konnnitiwa seaki")
And save with Ctrl + S </ kbd>.
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.
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.
Type ʻext install Python` and click on the many DL Pythons below about 5 to install.
During the installation, the bottom left is round and round.
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)
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
.
This will check your code for errors when you save it. (No need to restart VS Code)
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.
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.)
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.
(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. )
It's finally browser operation. DL chromedriver.exe. https://sites.google.com/a/chromium.org/chromedriver/downloads
Unzip and place chromedriver.exe in the same folder as hello.py.
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.
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
Save with Ctrl + S </ kbd> and then run with Ctrl + Shift + B </ kbd> Then, chrome will start up and search without permission. 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.
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