6.13python jobs

Today content summary:

1. The function portion

01.-defined functions in three ways
* No function parameter 
# not need to receive incoming external parameters
* There function parameter 
# needs to receive external parameters passed
Note: one more or less a parameter passing is not
* Empty function 
# encounter some of the more difficult to achieve the function, it can cause temporarily unable to continue writing code.
# It is generally in the production development, will implement all the functions defined as empty function.
' 
# # Nullary function 
# # not need to receive incoming external parameter 
# DEF foo (): 
#      Print (' .. from foo ') 
# foo () 
# 
# 
# 
# # has a function parameter 
# # needs to receive external incoming parameter 
# DEF Login (User, pwd): 
# 
#      Print (User, pwd) 
# # 
# # # a parameter passing one more or less non- 
# Login ( 'Tank', '123') 
# # Login ( ' tank ',' 123 ', 111 ) # multiple, given 
# # Login (' Tank ') # low, given 
# 
# # = 10 X 
# # Y = 20 is 
# # 
# # IF X> Y:
# # 
# # Print (X) 
# # 
# # the else: 
# # Print (Y) 
# 
# # compare two operand size 
# DEF MAX2 (X, Y): 
# 
#      IF X> Y: 
# 
#          Print (X) 
# 
#      the else: 
#          Print (Y) 
# 
# MAX2 (10, 30) 
#
View Code
02. The return value of the function 
when calling the function, the function generates results need to receive the internal body, the return value is returned.
# def max2(x, y):
#
#     if x > y:
#
#         return x
#
#     else:
#
#         return y
#
# res = max2(10, 5)
#
# print(res)
View Code
03. function object 
refers to the name of the function at the memory address.
# DEF FUNC (): 
#      Pass 
# 
# # Print (FUNC) # <function FUNC AT 0x101dd2e18> 
# # 
# # FUNC () 
# 
# DEF func2 (): 
#      Pass 
# 
# # the function object, passing in the dictionary 
# = {dict1 
#      '. 1': FUNC, 
#      '2': func2 
# } 
# 
# Choice = iNPUT ( 'enter number function:') Strip (). 
# 
# # == IF Choice '. 1': 
# # FUNC () 
# # elif Choice == '2': 
# # func2 () 
# 
# #
# # If the user selects the object corresponding to the key value of the function, the function is called 
# IF Choice in dict1: 
#      dict1 [Choice] () # dict1 [ '. 1']
View Code
04. nested functions: 
nested definitions:
in the function, defined functions.

Nested call:
 # Function nested definitions 
# 
# DEF func1 (): 
# 
#      Print ( 'func1 ...') 
# 
#      DEF func2 (): 
#          Print ( 'func2 ...') 
# 
#          DEF func3 (): 
#              Print ( 'func3 ...') 
# 
#              # .... 
# 
#          return func3 
# 
#      return func2 
# # internal function by the function value, the function call 
# func2 = func1 () 
# func3 = func2 () 
# func3 () 
# 
# 
# # nested function calls 
# DEF func1 ():
#
#     print('func1...')
#
#     def func2():
#         print('func2...')
#
#         def func3():
#
#             print('func3...')
#
#             # ....
#
#         func3()
#
#     func2()
#
# func1()
View Code
05. namespace 
python interpreter comes with: built-in namespace
from within the definition of py file, wore the leftmost defined: global name space
defined inside a function: local name space
= name ' Tank ' 

DEF func1 ():
     # name = 'Tank' 
    Print () 

    DEF func2 (): 

        Print ( ' func2 ... ' ) 

# Print (name, 'global print') 

func1 ()
View Code

2. The template package

# Import module name 
Import B

# from
# B module to import a file
# automatically execute the code in a file
# Import module name 
Import B 

# from 
# import a file module B 
# code that automatically performs a file 
from B Import a 

# the __name__: of Ba 
# a
View Code

3. Built-in templates

01.time
02.os
03.sys
04.json
'' '' '' 

'' ' 
Common modules (built-in modules) ' 
'' 

# time 
# Import time introducing time module # 
# # obtain a timestamp 
# Print (the time.time ()) 
# 
# # Wait 2 seconds 
# time. SLEEP (2) 
# 
# Print (the time.time ()) 

# OS 
Import OS
 # operating system files interact 
# determines whether the file exists tank.txt 
# Print (os.path.exists ( 'tank.txt') ) True # 
# Print (os.path.exists ( 'tank1.txt')) False # 
# Print (os.path.exists (r'D: \ python_files \ day03 \ tank.txt ')) True # 
# 
# # Get the current file in the root directory 
#Print (os.path.dirname (__ file__)) # D: / python_files / day03 

# SYS 
Import SYS
 # get python file path environment variable 
# Print (sys.path) 
# # to add items to the root directory environment variable in 
# sys.path.append (os.path.dirname (__ file__)) 
# Print (the sys.path) 


# JSON 
Import JSON
 # USER_INFO = { 
#      'name': 'Tank', 
#      'pwd': '123' 
# } 

# dumps: sequence of 
# 1, to switch the dictionary json data 
# 2, then the data is converted into a string json 
# RES = json.dumps (USER_INFO) 
# Print (RES)
# Print (type (RES)) 
# 
# with Open ( 'user.json', 'wt', encoding = 'UTF-. 8') AS F: 
#      f.write (RES) 

# loads: deserialize 
# JSON. loads () 
# . 1, the data files into memory json 
# with Open ( 'user.json', 'R & lt', encoding = 'UTF-. 8') AS F: 
#      # reading obtained string 
#      reached, f.read = RES () 
#      # Print (type (RES)) 
#      # loads to convert the string into json format type dict 
#      user_dict = json.loads (RES) 
#      Print (user_dict) # { 'name': ' Tank ',' pwd ':' 123 '} 
#      Print (type (user_dict)) # <class' dict'>


# dump
# user_info = {
#     'name': 'tank',
#     'pwd': '123'
# }
# with open('user_info.json', 'w', encoding='utf-8') as f:
#     # str1 = json.dumps(user_info)
#     # f.write(str1)
#     # dump: 自动触发f.write方法
#     json.dump(user_info, f)


# load
with open('user_info.json', 'r', encoding='utf-8') as f:
    # res = f.read()
    # user_dict = json.loads(res)
    # print(user_dict)

    # The Load: automatically trigger f.read () 
    user_dict = the json.load (f)
     Print (user_dict)
View Code

4. reptiles

http protocol: 
request url:
https://www.baidu.com/

request method:
GET

request header:
Cookie: may need attention.
User-Agent: to prove that you are a browser
NOTE: The browser request headers to find
01 crawling sites 
02 crawler pear video
# Requests module 
# PIP3 the install Requests 
# PIP3 Tsinghua source address of the install module name -i 
# PIP3 https://pypi.tuna.tsinghua.edu.cn/simple Requests the install -i 

# Import Requests 
# 
# Response = requests.get ( = URL 'HTTPS: //www.baidu.com/') 
# response.encoding = 'UTF-. 8' 
# Print (response) # <the response [200 is]> 
# # return a response status code 
# Print (response.status_code) 200 # 
# # return a response text 
# # Print (response.text) 
# Print (type (response.text)) # <class' STR '> 
# 
# with Open (' baidu.html ',' W ', encoding ='utf-8') as f:
#     f.write(response.text)



# 爬取梨视频
import requests
res = requests.get('https://video.pearvideo.com/mp4/adshort/20190613/cont-1565846-14013215_adpkg-ad_hd.mp4')

print(res.content)

with open('视频.mp4', 'wb') as f:
    f.write(res.content)
View Code

 



Guess you like

Origin www.cnblogs.com/bcsacr/p/11018711.html