Getting started with Python crawlers | 3 Crawler Essential Python knowledge

Source: Internet
Author: User

This is a Python crawler for small white free teaching course, only 7 section, let the zero basis of your initial understanding of the crawler, followed by the course content to crawl resources. Look at the article, open the computer hands-on practice, an average of 45 minutes to learn a section, if you want, today you can enter the reptile Gate ~

OK, let's start our third lesson, "Reptile must know Python"! Cheer up, look at the blackboard ~

    1. Strings (String)

1.1 Input string

When writing a string, you can use either single quotation marks (') or double quotation marks ("):

For multiline strings with line breaks, you can use the triple quotation marks ("' or" "):

1.2 Accessing values in a string

Python accesses substrings, you can use square brackets to intercept strings, as in the following example:

The result of the above instance execution:

1.3 Modification of the string

The string that already exists in Python cannot be changed by itself:

Error:

However, you can modify a string that already exists, and then assign it to another variable:

The result of the above instance execution:

1.4 Python object to string

Many Python objects can be converted to strings using the STR function:

The result of the above instance execution:

1.5 string as Sequence type processing

Because a string is actually a sequence of characters, it can be treated as a sequence type (such as a list, a tuple, and so on):

The result of the above instance execution:

1.6 Python escape character

When you need to use special characters in characters, Python uses backslash () escape characters, such as the following table:

1.7 String operators

The following table instance variable a value is the string "Hello" and the B variable value is "Python":

1.8 Common methods for strings

    1. Tuple (tuple)

2.1 Creating a tuple

Tuple (tuple) is a one-dimensional, fixed-length, immutable sequence of Python objects. Tuple creation is simple, just add elements in parentheses and separate them with commas:

To create an empty tuple:

When you include only one element in a tuple, you need to add a comma after the element:

By calling a tuple, any sequence or iterator can be converted to a tuple:

The result is:

2.2 Accessing tuples

Tuples can use the subscript index to access the values in the tuple, as in the following example:

The result of the above example output:

2.3 Modifying tuples

The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:

The result is:

2.4 Tuple operators

As with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.

2.5 Index and interception of tuples

Because tuples are also a sequence, we can access the elements at the specified location in the tuple, or we can intercept an element in the index as follows:

Meta-group:


2.6-tuple built-in functions

    1. Listing (list)

3.1 Creating a list

A list is longer than a tuple, and its contents can be modified. It can be defined by square brackets ([]) or by the list function:

The result is:

To modify the contents of a list that has already been defined:

The result is:

3.2 Accessing values in a list

Using the subscript index to access the values in the list, you can also use square brackets to intercept the characters as follows:

The result is:

3.3 Update List

You can modify or update data items for a list, or you can use the Append () method to add list items, as follows:

The result is:

3.4 Deleting list elements

You can use the DEL statement to delete the elements of a list, as in the following example:

The result is:

3.5 Determine if a value exists in the list

By using the In keyword, you can tell whether a list contains a value:

The result is:

3.6 List Script Operators

The list pairs + and operators are similar to strings. The + sign is used for the combined list, and the number is used for repeating lists.

3.7 List interception

The list of Python intercepts the type of string manipulation as follows:

3.8 list built-in functions & methods

    1. Dictionary (dict)

4.1 Creation of dictionaries

A dictionary is another mutable container model and can store any type of object.
Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces ({}), as shown in the following format:

The key must be unique, but the value does not have to be. The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
A simple Dictionary instance:

You can also create a dictionary like this:

4.2 Accessing values in the dictionary

Put the corresponding key into square brackets, as follows:

The result of the above example output:

If the data is accessed using a key that is not in the dictionary, the output error is as follows:

The result of the above example output:

4.3 Modifying a dictionary

The way to add new content to a dictionary is to add a new key/value pair, modify or delete an existing key/value pair as follows:

The result of the above example output:


4.4 Deleting a dictionary element

Del can delete a single element can also remove the dictionary, clear can empty the dictionary. The difference between del Delete dictionary and Clear empty Dictionary is that the former is the whole dictionary is deleted, the dictionary no longer exists, and the latter is to empty the contents of the dictionary, leaving a "shell."


4.5 dictionary built-in functions & methods

    1. Condition control

5.1 Article Control Flow

A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false). You can easily understand the execution of conditional statements by:

5.2 If statement

The general form of the IF statement in Python is as follows:

If "condition_1" is true, the "statement_block_1" block statement is executed
If "condition_1" is false, the "condition_2" will be judged
If "Condition_2" is true, the "statement_block_2" block statement is executed
If "Condition_2" is false, the "STATEMENT_BLOCK_3" block statement is executed
Attention:
1. Use a colon (:) after each condition to indicate the next block of statements to be executed after the condition is met.
2. Using indentation to divide the statement block, the same indentation number of statements together to form a statement block.
3. There are no switch–case statements in Python.
Give an If instance:

Execute the program and enter the age of the dog:

Program Run Result:

Exit the program after entering enter.

5.3 Common operator operators

5.4 If nesting

In a nested if statement, the IF...ELIF...ELSE structure can be placed in another if...elif...else structure.

To cite an if nested instance:

The input statements and results are as follows:

    1. Looping statements

6.1 Loop Statement Flow

The looping statements in Python have for and while. The control structure diagram for the Python loop statement is as follows:

6.2 While Loop

The general form of a while statement in Python:

Also be aware of colons and indents. The following instance uses while to calculate the sum of 1 to 100:

The results of the implementation are as follows:

Execute the above script and the output is as follows:

6.3 For statement

The general format for the For loop is as follows:

The break statement is used in the for instance to jump out of the current loop body:

The results are as follows:

In addition to using the break statement to jump out of the loop in the for And while loop body, we can also use the Continue statement, which is used to tell Python to skip the remaining statements in the current loop block and then proceed to the next round of loops.
The code blocks are as follows:

The results are as follows:

All right, here's the lesson!

Getting started with Python crawlers | 3 Crawler Essential Python knowledge

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.