I was going to find an introductory tutorial on the Internet, but since Python is rarely the first time programmers come into contact with the language learned by the program, the existing tutorials on the Internet are not very basic, so I decided to write them myself.
If there is no program foundation, you may feel that this article covers a lot of content. Compared with the teaching speed of C language that is often taught in universities, this article has about four or five lesson content. Compared with online program video tutorials, It is roughly equivalent to two or three hours of content; turning over a program book is equivalent to turning over an hour. Therefore, if you plan to study in depth, you are recommended to read books for efficiency.
It doesn't matter if you cannot understand some of the content in this article for the time being, because it is a basic knowledge that is often used, and will always be encountered during the actual coding process. There will be about two or three articles on practical code writing later, so you can choose to have a better impression of this knowledge at that time.
However, if you feel that technology is unnecessary for SEO and you are not interested in this article, how long will SEO be your career? If you plan to do it well, it is reasonable to spend at least tens of thousands of hours learning some technologies.
If you cannot understand the document because it is not clearly stated, please help. The first time I wrote a complete tutorial for the program class, it was inevitable that there were omissions.
Why Learning Python
If you decide to learn some technologies to assist SEO, Python is at least the first choice for programming languages in my opinion.
1. Getting started with Python is relatively simple
If anyone who has been familiar with C language in a college course but does not know much about the program, they may think that the program is as troublesome as C, after learning the elegant Python code, you will know that the C code is smelly and long. Even if you have never touched any program, Python itself is suitable for getting started with the program.
Although I wrote programs very early on, I was interested and never went deep into it at that time. Formal access to programs is a thing that will come into contact with SEO, after learning about a few hours, you will be able to write some simple collection programs. Up to now, as long as you can think of the needs, there are very few technologies that cannot be achieved. So there is nothing to do with the Foundation and the foundation, no matter what it is, you need to do it yourself.
Second, Python code is very flexible
Let's look at a very common code example. If there is no program foundation, you will not be able to understand the code, but it doesn't matter for the time being. You can also compare the code simplicity with the number of lines.
PHP:Copy codeThe Code is as follows: <? Php
$ List_1 = array (1, 2 );
$ List_2 = array ();
Foreach ($ list_1 as $ current ){
Array_push ($ list_2, $ current + 1 );
}
Print_r ($ list_2 );
?>
Python:Copy codeThe Code is as follows: list_1 = [1, 2]
List_2 = [current + 1 for current in list_1]
Print list_2
The first line of Python code is called "list Derivation", which provides many convenient language features such as this to make the code very concise.
Although flexible syntax is not always a good thing, flexible code writing means that the code written by everyone is often different in style, which leads to a wide gap in the level of team members and is not conducive to team development. However, the requirements for small SEO applications are generally personal development. At this time, code writing efficiency is more important than teamwork.
Iii. Extensive use of Python
Python is about 5-10 characters popular in programming languages. C #, Java, PHP, and so on are listed before it, but most of the time it is not because they are more powerful, but because of the reasons mentioned above, their code is not very flexible and suitable for team development, so they are more popular.
Generally, most network services that provide APIs provide Python interfaces (such as Baidu wangmeng and Amazon cloud services), while other languages that are stronger than Python do not have such wide support, for example, GO languages with more flexible code. (API interfaces of most Web services include C #, PHP, Java, Python, Ruby, and Perl. Therefore, select at least one language for learning)
There are also a lot of Python modules, and there are many existing modules, which means you can do less things. For example, to download a webpage, 5-10 lines of code are required in many languages, while Python requires only two lines:
Copy codeThe Code is as follows: import urllib2
Print urllib2.urlopen ('HTTP: // g.cn/'). read ()
What's interesting is that Google used to restrict internal development using the Python language. There are two reasons: 1. Python is used too much because of its high writing efficiency. 2. Python execution efficiency is slow. For SEO, it is the most important thing to pay attention to the writing efficiency, and generally do not need to care about the execution efficiency.
So far, there is a lot of nonsense, but interest is often the best motivation for learning. I hope there is no nonsense.
Installation environment and Configuration
Python programs are slightly different from normal programs. Generally, there is no exe file that can be directly double-clicked under Windows. Although it is not impossible to compile it into an exe file, it is relatively troublesome, in practice, this operation is rarely performed.
A Python program is a piece of text code that is saved to a file with the suffix ". py. After installing the Python runtime environment on your computer, you can run the py file. (In fact, many programs depend on the runtime environment. The most common one is. net Framework, but this is pre-installed on Windows, which is generally invisible)
For Windows systems, the runtime environment needs to be installed. For Linux systems, Python is generally provided without configuration.
Download link for Python runtime environment: http://python.org/getit/
On this page, we usually find and download Python 2.7.x Windows Installer (Windows binary-does not include source) at the first download link)
Note that it is best to download version 2.7.x (x represents any number, and the major version 2.7 is similar), while Python 3 and later versions are significantly different from previous versions, it is not recommended for beginners.
After the download and installation, there is nothing to pay special attention. Generally, it is installed in the default path c: \ python27 \. Do not place it in a directory-level folder, otherwise it will be troublesome to run.
Operating System
As mentioned above, Python can run in Windows and Linux systems, and it also supports more systems, such as Mac, you can even write and run Python programs on jailbroken iPhone and other devices.
However, the main strength of Python lies in its module support. With the module, you can do a lot of things conveniently. The configuration of modules in Windows and other systems is often troublesome, and Linux is often much easier. Therefore, after the entry stage, we recommend that you develop modules in the Linux environment.
There are three options for building a Linux environment:
1. install Linux on the local machine. It is suitable for people who have been working in Linux for a long time and need to have a certain understanding of the basic knowledge of Linux. The main drawback is that some programs can run in Windows, and occasionally need to switch the system. I mainly work in this environment.
2. Install VMware and other virtual machines in Windows, and then install and use Linux in the virtual machine. Suitable for beginners, but because of the relationship between virtual machines, unless the computer is well configured, the operating efficiency of both systems will be relatively low. Moreover, virtual machines are not conducive to creating an atmosphere for learning Linux. Most people tend to use Windows instead of Linux when using virtual machines. I personally do not recommend this option.
3. purchase a Linux VPS and use SSH for remote operations. The main advantage is that you can easily connect to the server at work and at home, without bringing programs and data with the USB flash drive every day; you can also use Linux without affecting Windows. However, the disadvantages are equally obvious: no graphical interface is unfriendly to the publisher; because of the network delay, the efficiency of code writing is often affected. This solution is recommended for beginners.
For purchasing VPS, we recommend Linode or PhotonVPS. About 50-RMB a month can buy a VPS that can be used to write common programs, or two or three small websites.
Linux has many releases available for installation, such as Ubuntu and Fedora.
We recommend that you install Fedora for the purpose of setting up a Linux environment on the local machine. It is similar to the Red Hat system, which is the most common CentOS system on the server. It is very helpful to be familiar with CentOS.
If Linux is installed locally and sometimes used for entertainment such as video watching, we recommend Ubuntu, which is slightly better than Fedora in daily use.
CentOS is recommended for building Linux on VPS. Because CentOS is the most widely used, various software provides better support for it.
Corresponding to solution 1 in the Linux environment, general use a USB flash drive to load the iso image for installation; for solution 2, it is generally directly installed by the virtual machine software to load the iso image; for solution 3, generally, you can directly select an operating system on the VPS background without installing it yourself, which is convenient.
Editor
Before writing code, you must first have an appropriate editor. Although the notepad that comes with Windows is not unusable, it is very difficult to write programs.
For the Windows system editor, I recommend Notepad ++ for its simplicity. Of course, you can also choose the more popular UltraEdit.
You can also use IDE, which is a collective term used to develop the integrated environment of software projects. It has great advantages in developing large projects, but may be slightly inflexible in dealing with small demands. Eclipse is better in IDE, and Python program can be developed with the PyDev plug-in. It can be used in both Windows and Linux.
If you usually need to write a large amount of code or have confidence in your learning ability, we recommend Vim for Linux and gVim for Windows (Vim GUI ). It is very different from writing code in a General Editor. It usually takes several days of study and several months of skilled time, but the efficiency of writing code is generally much faster.
Start Programming
Hello World!
"Hello World" was one of The most classic books in The computer field-The C Programming Language, which was widely used later ." The purpose of the "Hello World" program is to output a "Hello World" on the screen. Although it is very simple, it is of great significance when it is the first program written by everyone.
Open the editor and enter a line: (friendly prompt: copy and paste negative memories)Copy codeThe Code is as follows: print "Hello World! "
Complete. Save the file to c: \ hello. py (the path is random ).
Press the Win + R shortcut key, open "run", Enter CMD, press enter, and open the command line prompt. (For the Linux GUI, press Ctrl + Alt + T to open the terminal)
Input:
(Windows)
C: \ python27 \ python.exe c: \ hello. py
(Linux)
Python path/hello. py
You can see that the program is running and a line of "Hello World!" is displayed !". So far, the first program has been completed.
Variables and assignments
Copy codeThe Code is as follows: s = 'Hello World! '
Print s
Program output:
Hello World!
The code above, s is called a variable; the first line of the operation is called a value assignment.
Another example is to deepen your impression:
Copy codeThe Code is as follows: a = 1
B = 2
Print a + B
Program output:
3
Function
Copy codeThe Code is as follows: def hello (name ):
S = 'hello' + name + '! '
Return s
Print hello ('World ')
Print hello ('semwatch ')
Program output:
Hello world!
Hello semwatch!
A function is used to encapsulate a series of behaviors. One or more parameters are passed to the function (def hello (name), and then the function returns a calculated value (return s ).
Print hello ('World') is equivalent to outputting the calculation result after the hello function is run, that is, outputting Hello world!
Let's look at this example to learn more:
Copy codeThe Code is as follows: def add_num (a, B)
Return a + B
Print add_num (1, 2)
Print add_num (2, 3)
Program output:
3
5
Loop and judgment
The for loop, while loop, and if judgment mentioned later are the basis of all programming languages and must be fully understood.
(Python does not have the do while loop that is often used in other languages, nor does it have to be determined by the switch case)
For Loop:
PHP example:Copy codeThe Code is as follows: for ($ I = 0; $ I <10; $ I ++ ){
Echo "$ I \ n ";
}
?>
Here is an example of PHP, because the for Loop in most programming languages is similar to the syntax in this example.
It should be easy to understand, assign 0 ($ I = 0) to variable I, + 1 ($ I ++) at each loop ), when I is less than 10, it always loops ($ I <10), so it loops 10 times.
The echo statement outputs the numbers in each loop. In this example, the numbers 0-9 are output in sequence, but there is no need to study the echo syntax. You only need to understand the concept of the for loop.
The for loop Syntax of Python is quite different, but it is also very concise:
Copy codeThe Code is as follows: for I in range (10 ):
Print I
The two statements will output 0-9 numbers in sequence, and the print statement will automatically output a new line each time.
While loop:
Copy codeThe Code is as follows: I = 0
While I <10:
I = I + 1
Print I
The code means that I is assigned 0 values first, and when I is less than 10, the content in the loop body is continuously executed. Add 1 to the value of I and output I.
The output result is 0-9 digits in sequence.
If judgment:
Copy codeThe Code is as follows: I = 1
If I <2:
Print "I <2"
Else:
Print "I> = 2"
Program output:
I <2
The program first assigns 1 to I and then determines whether I is less than 2. If I is less than 2, It outputs I <2, otherwise the output I> = 2.
In addition, the equal and not equal judgment symbols are as follows:
Copy codeThe Code is as follows: I = 1
If I = 1:
Print "I = 1"
Elif I! = 1:
Print "I! = 1"
Equals = symbol, which is easy to confuse. Using the = symbol may cause some problems in most languages, but in Python, it is directly regarded as a code error. Not equal to the symbol! =, Can also be used <>, but the <> number is less and less used in today's programming languages, and may be discarded in later versions of Python, so it is generally recommended to use it! =.
In the code above, the third line of elif is short for else if. If I = 1 is not satisfied, continue to judge I! = 1.
List and Dictionary
In most languages, an array is a variable with multiple values. Python does not. It is similar to "list" and "Dictionary.
If you have PHP basics, you can quickly understand them by looking at the following comparison:
List:
PHP
Copy codeThe Code is as follows: $ l = array (1, 2, 3 );
?>
Python
Copy codeThe Code is as follows: l = [1, 2, 3]
Dictionary:
PHP
Copy codeThe Code is as follows: $ d = array (
'A' => 1,
'B' => 2,
'C' => 3,
);
?>
Python
Copy codeThe Code is as follows: d = {
'A': 1,
'B': 2,
'C': 3,
}
Without other language basics, it may be more appropriate to understand the list and dictionary through actual code.
List:Copy codeThe Code is as follows: l = [1, 2, 3]
For current in l:
Print current
The running result will output 1-3 numbers in sequence. (There is no PHP foreach syntax in Python, and it is all done using)
Dictionary:
Copy codeThe Code is as follows: d = {
'A': 1,
'B': 2,
'C': 3,
}
For key in d:
Print key
Print d [key]
The running result is a1b2c3, which is displayed in six rows.
The dictionary structure is as follows:
Dict = {key: value, key2: value2 ,...}
The for loop can assign the corresponding key to the variable during each loop. To access the corresponding value, you need to use something similar to dict [key, find the value of the corresponding key in dict.
Similarly, the List has similar operations, such:
Copy codeThe Code is as follows: l = [1, 2, 3]
Print l [1]
Running output:
2
It indicates the output of the 1st items in list l. It should be noted that for the vast majority of programming languages, the calculation starts from 0th items. Here, l [0] is 1, l [1] is 2, and l [2] is 3.
String and slice
A string is a string of characters, such as its name. For example, print "Hello World !" Is the output string Hello World !. The strings in Python are special. They belong to the sequence type like the list. Many usage methods are the same as the list.
Copy codeThe Code is as follows: s = "abc"
Print s [1]
The running result outputs B.
According to this feature, some concise code application can be derived. For example, to output "Hello World !" The first five characters of this string are similar to PHP in most languages:
Copy codeThe Code is as follows: $ s = "Hello World! ";
Echo substr ($ s, 0, 5 );
?>
Python is much simpler:
Copy codeThe Code is as follows: s = "Hello World! "
Print s [: 5]
Both sections of code output Hello.
S [: 5] uses the "slice" feature of Python. The complete method should be s [0: 5], but 0 can be omitted, the output content of this sequence index number starts from 0 to 5.
Of course, the list can also be sliced, such:
Copy codeThe Code is as follows: l = [1, 2, 3]
Print l [1:]
Output [2, 3] After running.
It means that the index number 1 is output to the end of the list.
Knowledge Point Summary
Add some new knowledge points to all the previous content and combine them into a piece of code. If you think about how the output results come out, you can better understand them. Because there are many knowledge points, you can not solve them for the moment, but these are the basic content in any programming language, and ultimately must be mastered.
In the code, # indicates the beginning of the annotation, And the content will not be run.
In addition, the first line of the Code declares that the code is encoded as UTF-8. Because the comments contain non-English characters, you need to specify the encoding. Otherwise, the program will report an error.
Copy codeThe Code is as follows: #-*-coding: UTF-8 -*-
Str_1 = 'hello'
Str_2 = 'World! '
# The following statements output Hello World!
# The comma in the print statement means that they are separated by spaces instead of line breaks.
Print str_1, str_2
# The following statements have the same effect as the preceding one.
Print str_1,
Print str_2
# You can also use a string connector.
Print str_1 + ''+ str_2
# You can also use the format operator, which is more commonly used than the former.
# Replace the following variables with % x
# Replace % s with a string. If an integer is replaced with % d, the decimal number is % f.
Print '% s % s' % (str_1, str_2)
#00 11 22 will be output
For I in range (3 ):
Print '% d % d' % (I, I ),
Print ''# output an empty string for line feed
# Output 11 22 33
For I in range (1, 4 ):
Print '% d % d' % (I, I ),
Print''
#00 22 44 will be output
For I in range (0, 5, 2 ):
Print '% d % d' % (I, I ),
Print''
#0 1 0 1 0
I = 0
While I <5:
I + = 1 # equivalent to I = I + 1
If I % 2 = 0: # if the remainder of I Division 2 is 0
Print 1,
Else:
Print 0,
Print''
# Outputs c
I = 3
If I = 1:
Print 'A'
Elif I = 2:
Print 'B'
Elif I = 3:
Print 'C'
Else:
Print 'error'
# Output 1 2 3 3 4 4 5 6
For I in range (10 ):
I + = 1
Print I,
If I <3:
Continue # If I <3, the loop is interrupted and re-run from the header of the loop body.
Elif I> 5:
Break # If I> 5, the entire loop is interrupted.
Print I,
Last
Although we are talking about programs, SEO involves far more than programs. Although I would like to mention all the aspects that should be learned from the beginning, I can't finish writing a few books, but I can't even use a few articles.
In the next article, some knowledge points will be skipped. If the reader does not have any basic knowledge, you need to learn it on your own. Which will be skipped later include but are not limited:
1. Basic Web knowledge, such as non-ASCII character set URL needs encoding and other knowledge: http://www.w3school.com.cn/html/html_urlencode.asp
2. For the use of browser Developer Tools, you can select Chrome developer tools or Firefox Firebug plug-ins. Chrome is recommended for personal use. In particular, you need to first understand its packet capture function, which is often used for data collection. The packet capture function is displayed on the "Network" tab in the Chrome developer tool.
3. Configure Python's pycurl module independently. There are two main options here. One is to install Curl on a Windows system, then install setuptools, and finally install the module. It is not recommended if it is more troublesome, in the future, other modules may be difficult to configure in Windows. Another option is to configure a Linux environment. In CentOS, Fedora, and other systems, run sudo yum install python-pycurl. In Ubuntu and other systems, run sudo apt-get install python-pycurl. This module will be used in a lot later, so be sure to configure it. Linux entry command visible: http://ooxx.me/common-ssh-commands.orz
4. Understand regular expressions. It is a powerful tool used to process strings. You can search for information on Baidu to get a preliminary understanding of it. At least you must first know it .*? And [\ s \ S] *? These two meanings are most commonly used in data collection. This is a good but difficult regular tutorial, may not very suitable for beginners: http://manual.phpv.net/regular_expression.html
In addition, it is best to buy a Python book. Although SEO does not have to be deeply involved in programs, the majority of knowledge on most program books is not very important. For example, every book will spend a lot of time introducing the idea and implementation of object-oriented programming, but most SEO needs only a few hundred lines of code, which is generally not involved. However, in some basic knowledge learning at the beginning, it is easy to omit or forget the knowledge point. It would be much better to have a book.
No Python books suitable for SEO have been found. I have a "Python core programming" on hand. I should say yes. If you are not sure which to buy, you can go to this forum to find the pdf documentation, it above all kinds of computer books should be relatively the most complete: http://club.topsage.com/forum-300-1.html
But do not read only pdf files. Generally, if you want to learn at least one language, you must prepare a paper book. It should be a tool book that you can refer to at any time after you are not clear about or forget the knowledge points.
Author: ZERO
This article is from semwatch. Please indicate the source link and author.