First , Must admit Python It is an elegant 、 An easy-to-use programming language . Often with a small amount of code , Can help you accomplish a beautiful thing . This is me too
Use python Years of words from my heart . More difficult than that 、 Troublesome software ,python Realized my program dream .
The beginning of learning Python, It doesn't need to be too complicated . Just have fun , Develop interest slowly , When you get started , You will learn more confidently .
Let's play today ,5 What line of code can do ? Here I'd like to introduce two fun projects , Introduce the following :
• Item 1 :5 Lines of code make the computer never stop !
• Item 2 :5 Line code crawl table data !
as everyone knows , When your computer mouse keeps moving , The system will default that your computer is working , So there will be no screen .
Since it's fun ? You don't have to , Why “ Do not set the constant screen ”? like that , You win , I have nothing to say . ha-ha !
Here is to use code to automatically control the mouse , Realize the function of mouse “ The up and down or so ” Mobile operating , Let the computer mistakenly think that it is operating the mouse .
ad locum , We use Python Medium pyautogui library . If this library is not installed on your computer , You can use the following code to install .
pip install pyautogui
Control mouse movement , It uses pyautogui In the library moveRel(x,y) function , Usage is as follows :
• meaning : According to the current location , Move the mouse pointer relatively ;
• Be careful : When x/y Greater than 0, It means to the right / Move the mouse pointer down . When x/y Less than 0, To the left / Move the mouse pointer up 【x/y Is an integer 】;
Of course, this random number , Definitely don't set it up for you , We also randomly generate . Here I need to introduce random A function in the library
random.randint(a,b), Usage is as follows :
• meaning : Used to generate random integers within the specified numerical range ;
• Be careful : This function takes two arguments a and b, Indicates the upper and lower limits of the specified interval ;
import random
random.randint(a=100,b=300)
give the result as follows :
With these foundations , Let's go straight to the code !
# Import related libraries
import pyautogui
import random
import time
Python Exchange of learning Q Group :906715085###
# Use while True loop , Keep the program running !
while True:
x = random.randint(-200,200)
y = random.randint(-200,200)
pyautogui.moveRel(x,y)
time.sleep(5) # Let the mouse move to a certain position , Stay for a few seconds , I'm afraid it's too tired
Of course ,pyautogui Library as a library for automatic mouse operation , There are many knowledge points worth digging . Here is just a simple use , Other knowledge can be based on your reality
International demand , Go and learn systematically .
If you want to learn Python Reptilian words , I think this should be the simplest crawler code .
To put it bluntly , Namely pandas Library crawls table data . This is actually the same as Excel It's kind of like ,Excel It's just that you can only crawl some regular table data ? But this one works better , After all, I won't let you do it .
Today we climbed to the web page is “ China business intelligence network ”.
Address the following :
https://s.askci.com/stock/a/0-0?reportTime
If you want to use it pandas Crawl data , its HTML The structure is the following Table Format .
<table class="..." id="...">
<thead>
<tr>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>...</td>
</tr>
<tr>...</tr>
<tr>...</tr>
...
<tr>...</tr>
<tr>...</tr>
</tbody>
</table>
We click F12, View the source code , When we locate the data to be crawled , You will find that the data meets this characteristic .
There is no need to talk about other knowledge , After all, are pandas Knowledge in , Everyone is familiar with . Code up :
# Import related libraries
import pandas as pd
import csv
# We only crawl ten pages of data
for i in range(1,10): # Crawl all pages
tb = pd.read_html(f'http://s.askci.com/stock/a/?reportTime=2021-03-31&pageNum={
i}')[3]
tb.to_csv(r' The listed company .csv', mode='a', encoding='utf_8_sig', header=1, index=0)
give the result as follows :
Proper , Data from so many listed companies , It's in your pocket . Then you can do a simple analysis , Or practice pandas Data processing , Is it not fragrant ?
Another point worth noting here is , There is one in the code above [3], What does it mean ?
This is because there may be multiple tables on the web page , At this time, we need to rely on the slice of the list tables[x], To specify which table to get .
Okay , That's all for today's case , Have you learned ?