Slice
# # Slice
# s = 'hello word'
# # The subscript index is 0
# print(s[0]) #h
# # Left closed and right open (left starts with the subscript, right is several index values), for example, 4 index values starting from 0
# print(s[0:4]) #hell
# # Change the starting position of the starting value
# print(s[1:]) #ello word
# # End position of subscript
# print(s[:5]) #hello
# #From the position with subscript 0 to the end of position with subscript 6, the length increases by 2 each time
# print(s[0:6:2]) #hlo
Dictionary traverse delete add modify query
# Dictionary query
# preson = {'name':'Zhou Agou','age':18}
# # inquiry mode
# print(preson['name']) #Zhou Agou directly prints the value of name
# print(preson.get('age')) #18 You can use get to get the value
# # print(preson["sex"]) #Report an error because there is no sex KeyError: 'sex'
# Dictionary modification preson['name'] Select some data to modify. If there is no data, add it and modify it.
# preson = {'name':'Zhou Agou','age':18}
# print(preson) #{'name': 'Zhou Agou', 'age': 18}
# preson['name'] = 'Zhang San'
# print(preson) #{'name': 'Zhang San', 'age': 18}
# # Dictionary addition If preson['sex'] does not exist, add it. If it does not exist, add it. If it does not exist, just modify it.
# preson = {'name':'Zhou Agou','age':18}
# print(preson) #{'name': 'Zhou Agou', 'age': 18}
# preson['sex'] = 'male'
# print(preson) #{'name': 'Zhou Agou', 'age': 18, 'sex': 'Male'}
# preson['name'] = 'Zhang San'
# print(preson) #{'name': 'Zhang San', 'age': 18, 'sex': 'Male'}
# # Dictionary deletion del cannot delete objects (preson) clear deletes all but retains objects (brackets)
# preson = {'name':'Zhou Agou','age':18}
# print(preson) #{'name': 'Zhou Agou', 'age': 18}
# del preson['age']
# print(preson) #{'name': 'Zhou Agou'}
#
# preson.clear() #Delete all but keep the brackets
# print(preson) #{}
# # Dictionary traversal data
# preson = {'name':'Zhou Agou','age':18}
# print(preson) #{'name': 'Zhou Agou', 'age': 18}
# # Traverse all key values of preson
# for key in preson.keys():
# print(key) # name age
# # Traverse all values of preson
# for value in preson.values():
# print(value) # Zhou Agou 18
# # Traverse all key values of preson
# for key,value in preson.items():
# print(key,value) # name Zhou Agou age 18
# # Traverse all item values of preson
# for item in preson.items():
# print(item) #('name', 'Zhou Agou') ('age', 18)
Define functions
# # Define function
# # Exercise 1
# def f1():
# print('nihao')
# print('woshixiaozhou')
# # Call functions
#f1()
# # Exercise 2
# def sun():
# a = 1
# b = 2
# c = a + b
# print(c)
# sun()
# # Exercise 3
# def sun(a,b): #(formal parameter)
# c = a + b
# print(c)
# # Arguments
# sun(100,20)
Function return value
# Function return value
# # Exercise 1
# def sun():
# return 'ice cream'
#
# # Assign the return value of sun to food
# food = sun()
# # Two forms of printing
# print(food)
# print(sun())
# # Exercise 2
# def sun(a,b): #(formal parameter)
# c = a + b
# return c
# # Assign the return value of sun to food or assign it to output a new value
# food = sun(10,50)
# # Two forms of printing
# print(food)
# # Or output with value
# print(sun(10,50))
Scope
# Scope
# Global scope
# a = 100
# def sun():
# print(a)
#
# sun()
#local scope
# def sun():
# a = 120
# print(a)
# sun()
Serialization
Exception error
# How to write exception prompts
# try:
# Possible exceptions
# except exception type
# Friendly reminder
# try:
# fp = open('text.txt','r') #Open this file
# fp.read() #read
# except FileNotFoundError:
# print('No such')
urllib usage
#Introduce urllib
import urllib.request
# define path
url = 'https://baike.baidu.com/'
# The simulator sends a request to the server
response = urllib.request.urlopen(url)
#(3) Get the source code content of the page in the response. The meaning of the content
# #The read method returns binary data in the form of a word book
# So we need to convert the hexadecimal data into a string
#Binary--》String decoding decode('encoding format')
# Get the corresponding source code
content = response.read().decode('utf-8')
#Print data
print(content)
One type, six methods
# One type and six methods
# import urllib.request
# # Define path
# url = 'https://baike.baidu.com/'
#
# response = urllib.request.urlopen(url)
# One type and six methods
# response is HTTPResponse
# print(type(response)) #<class 'http.client.HTTPResponse'>
# Read all bytes one byte at a time
# content = response.read()
#print(content)
# Read byte by byte. read() can write how many bytes you want to read.
# content = response.read(4)
# print(content) #b'<!DO'
# # Read byte by byte readline() can only read one line
# content = response.readline()
# print(content) #b'<!DOCTYPE html>\
'
# # Read all byte by byte readlines()
# content = response.readlines()
# print(content) #b'<!DOCTYPE html>\
'
# # Return status code 200 means successful return
# print(response.getcode()) #200
# # Return path
# print(response.geturl()) #https://baike.baidu.com/
# # Get status information
# print(response.getheaders()) #[('Content-Type', 'text/html; charset=UTF-8'),
Download video audio pictures
# Download
# import urllib.request
# Download web page
# define path
# url = 'https://baike.baidu.com/'
# urllib.request.urlretrieve(url,'baike.html')
# Download image
# https://img0.baidu.com/it/u=3021883569,1259262591 & amp;fm=253 & amp;fmt=auto & amp;app=120 & amp;f=JPEG?w=1140 & amp;h =641
# url_img = 'https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg'
# urllib.request.urlretrieve(url_img,filename='lisa.jpg')
#Download video
# https://vd2.bdstatic.com/mda-picyuf5c6sekhy5c/720p/h264/1694646650126259454/mda-picyuf5c6sekhy5c.mp4?v_from_s=hkapp-haokan-hbf & amp;auth_key=1694690882-0-0-275f5 e093e7c5d6af95b829765e00417 &bcevod_channel =searchbox_feed & amp;pd=1 & amp;cr=2 & amp;cd=0 & amp;pt=3 & amp;logid=1682821669 & amp;vid=1074113355220740733 & amp;klogid=1682821669 & amp;abtest=112751_3
# url_v = 'https://vd2.bdstatic.com/mda-picyuf5c6sekhy5c/720p/h264/1694646650126259454/mda-picyuf5c6sekhy5c.mp4?v_from_s=hkapp-haokan-hbf & amp;auth_key=1694690882-0-0 -275f5e093e7c5d6af95b829765e00417 & amp;bcevod_channel=searchbox_feed & amp;pd=1 & amp;cr=2 & amp;cd=0 & amp;pt=3 & amp;logid=1682821669 & amp;vid=1074113355220740733 & amp;klogid=1682821669 & amp; abtest=112751_3'
# urllib.request.urlretrieve(url_v,'video.mp4')
Pictures
Video
Web page
Customizing the request object is to prevent anti-crawling
# Custom request object
import urllib.request
# define path
url = 'https://baike.baidu.com/'
#ua request header
headers ={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 Edg/116.0.0.0"}
# And because the dictionary cannot be stored in the urlopen. method, headers cannot be passed in #Defining the request object
# #Note that because of the parameter order problem, you cannot directly write the url and there is data between headers, so we need to pass parameters using keywords.
# To prevent anti-crawling, you must carry request headers
request = urllib.request.Request(url = url ,headers = headers)
# Simulate the server to send a request
response = urllib.request.urlopen(request)
# Read html page data and obtain the corresponding source code
content = response.read().decode("utf8")
print(content)
Request quote
import urllib.request
import urllib.parse
# define path
url = 'https://baike.baidu.com/'
#Request header
headers ={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 Edg/116.0.0.0"
}
#Object to search for
name = urllib.parse.quote('Baidu')
#splicing path
url = url + name
request = urllib.request.Request(url=url,headers=headers)
# Simulate the server to send a request
response = urllib.request.urlopen(request)
# Read html page data and obtain the corresponding source code
content = response.read().decode("utf8")
print(content)