Life is too short, I use python 2

Common code framework for today's topic crawling web pages

We first open IDLE and select the File->new window command (or you can directly press the Ctrl+N key, which in many places means a new file)

We fill in the code:

 

To save the file is to press the shortcut key Ctrl+S to save the source code as a hello.py file

Press F5 to run (at the same time we can select the Run->Run Module command)

An error appears in IDLE:

The reason is that name '_name_' is undefined

Note: a long underscore is required here to make it correct to distinguish the syntax of python itself

After the correction we run again:

 

We see the same result as before

Let's modify the web page connection and see what happens:

 

 The result is "generates an exception"

Explain the general code framework: the get method requests a link, and r.raise_for_status is used to determine whether the request is normal (that is, 200), otherwise an exception will be generated. The sentence r.encoding = r.apparent_encoding can be our request to return the webpage decoding is correct, and finally return the content of the webpage.

This framework is used to solve our problems accessing web pages.

 

 

Today's content is like this, a little bit every day, focusing on persistence. come on! ! !

 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937959&siteId=291194637