爬虫学习前记----Python

引言

1.语言:python

2.学习资源:【Python+爬虫】

3.爬虫日记:

python内容

1.字符串输出

(1)引号问题

python 复制代码
print("python")
输出:python

print('python')
输出:python

print('python"学习"')
输出:python"学习"

print("python\"学习\"") #转义
输出:python"学习"

(2)换行问题

python 复制代码
-----print函数自动换行-----
-----    \n 增加换行 -----

输出:python
     学习

【错误的示例】
print("python
学习") 

【正确】
print("""python
学习""")

(3)字符串拼接

python 复制代码
print("python"+"学习")
输出:python学习

(4)输出变量内容

python 复制代码
hello="您好!"
print(hello + "我是李明")
输出:您好!我是李明

2.函数调用

在文件开头引用'库'

在调用时格式为:库名.库函数(参数)

python 复制代码
import math

math.sqrt(4)

3.注释

(1) #

bash 复制代码
# print(1)
# print(2)
# print(3)

注:多段代码被注释掉时,可以用快捷键Ctrl+/

(2)写入长注释时

python 复制代码
'''
你好
你好
这是注释
'''

"""
这也是
注释
"""

4.字符串

(1)len获取长度

python 复制代码
a = len("\n")     ---1
b = len("你好")   ---2
c = len("hello")  ---5

(2)字符串的某个字符

python 复制代码
d = "abcdef"
print(d[1])  ---b

5.type数据类型

(1)type查看数据类型

python 复制代码
print(type("abc")) ---<class 'str'>
print(type(True)) ---<class 'bool'>

(2)转换

python 复制代码
str(123)
int("456")

6.输入

python 复制代码
i = input("请输入")

注意:输入的为字符串格式

7.if

python 复制代码
i = input("请输入")

if int(i) == 1:
    print("接收到了1")
elif 1 < int(i) <= 10:
    print("接收到1<x<10")
else:
    print("没有接收到目标")

8.逻辑运算

优先级:not \> and \> or

(1)or

python 复制代码
if i == 1 or i == 2 or i == 3:
    print("接收到了{1,2,3}")

(2)and

python 复制代码
if i == 1 and j == 0:
    print("i=1 j=0")

(3)not(只能接一个)

python 复制代码
if not i == 1:
    print("没接收到1")

9.列表

python 复制代码
''' 在列表末尾添加新的对象 '''
list1.append(1)  
''' 统计某个元素在列表中出现的次数 '''
list1.count(1)  
''' 拼接 '''
list1 = list1 + list2
''' 从列表中找出某个值第一个匹配项的索引位置 '''
list1.index(11)  
''' 将100插入列表index=0 '''
list1.insert(0, 100)  
''' 移除列表中100的第一个匹配项 '''
list1.remove(100)  
''' 反向列表中元素 '''
list2.reverse()  
'''  对原列表进行排序 '''
list1.sort() 
''' 读取列表中第三个元素 '''
print(list1[2]) 
''' 读取列表中倒数第二个元素 '''
print(list1[-2])  
''' 从第二个元素开始截取列表 '''
print(list1[1:])  

10.字典

python 复制代码
dict1={"1":"yi"}
dict1["2"]="er"

注意 a:b键值对中: a是键,b是值 。

键还能是元组:

python 复制代码
dict1={"1":"yi"}
dict1["2"]="er"
tuple1=("张伟",18)
tuple2=("张伟",19)
dict1[tuple1]="137000000"
dict1[tuple2]="180000000"

11.range

python 复制代码
计算:1 + 2 + ... + 100
for i in range(1, 101):  
    total = total + 1

等价于

cpp 复制代码
for(int i=1;i<101;)
    total+=i;

12.while

python 复制代码
while i >= 100:
    print("i>=100")

13.类

python 复制代码
class Employee:
    def __init__(self, name, jd):
        self.name = name
        self.id = id

    def print_info(self):
        print(f"员工名字:{self.name},工号:{self.id}")


class FullTimeEmployee(Employee):
    def __init__(self, name, id, monthly_salary):
        super().__init__(name, id)
        self.monthly_salary = monthly_salary

    def calculate_monthly_pay(self):
        return self.monthly_salary


class PartTimeEmployee(Employee):
    def __init__(self, name, id, daily_salary, work_days):
        super().__init__(name, id)
        self.daily_salary = daily_salary
        self.work_days = work_days

    def calculate_monthly_pay(self):
        return self.daily_salary * self.work_days

(1)类构造

class name:

def inti(self,其他参数...):

构造函数操作...

(2)类继承

class A(B): #A是继承了B的子类

(3)子类继承且改编父类的构造函数

def init(self, name, id, daily_salary, work_days):

super().init(name, id)

其他操作...

14.文件

(1)路径

绝对路径

/user/demo/data.txt

相对路径

../tmp

(2)open("a.txt","r",encoding="utf-8")

python 复制代码
f = open("a.txt", "r", encoding="utf-8")
print(f.read())
f.close()

注:当前这次读完后,系统会记录读到的位置,然后下次调用read函数将返回继续增加的内容。读完后一定要close!

或者(with as 自动关闭):

python 复制代码
with open("a.txt") as f:
    print(f.read())

读字节

python 复制代码
print(f.read(10))  # 指定10字节

读一行 --可搭配while使用

python 复制代码
print(f.readline())  

全部读

python 复制代码
lines=f.readlines()
for line in lines:
    print(line)

(2)open("a.txt","w",encoding="utf-8")

特点:

(1)区别于"r":只写模式下,若没找到指定文件,则创建文件。

(2)"w"模式下,write函数覆盖掉文件中所有内容!

(3)write函数不自动加换行,需手动\n

(4)不能读原本的内容(即read函数)

(3)open("a.txt","a",encoding="utf-8")

特点:

(1)区别于"w":附加模式下,write函数在文件内容的末尾写入

(2)write函数不自动加换行,需手动\n

(3)若没找到指定文件,则创建文件。

(4)不能读原本的内容(即read函数)

(4)open("a.txt","r+",encoding="utf-8")

(1)可读可写

(2)write函数不自动加换行

(3)若没找到指定文件,会报错!

15.捕捉异常

python 复制代码
try:
    user_weight = float(input("请输入您的体重(单位:kg)"))
    user_height = float(input("请输入您的身高(单位: m):"))
    user_BMI =user_weight /user_height ** 2
except ValueError:
    print("输入不为合理数字,请重新运行程序,并输入正确的数字。")
except ZeroDivisionError:
    print("身高不能为零,请重新运行程序,并输入正确的数字。")
except:
    print("发生了未知错误,请重新运行程序。")
else:
    print("您的BMI值为:"+str(user_BMI))
finally:
    print("程序结束")

16.测试代码unittest

Python之unittest框架的介绍及使用_python unittest abc-CSDN博客

相关推荐
不是万能的豆学长15 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
gCode Teacher 格码致知16 小时前
Pandas教学-18:number与numeric的区别
python·pandas
梦想不只是梦与想19 小时前
Python 中subprocess 模块:执行外部命令
python·subprocess·调用外部命令
懂点Tech19 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
Canmag 兴隆磁性20 小时前
中大型钕铁硼产品的磁通测量与自动分选:一条产线自动化的工程实现思路
运维·学习·自动化·永磁材料
小黄人软件1 天前
桌面需要整理吗?有工具自动分类
学习
老衲不用@飘柔1 天前
SFP与SFP+学习知识整理
学习
X54先生(人文科技)1 天前
社会嵌入式终身学习机器与修复时代-ELR架构和技术演化探讨
人工智能·深度学习·学习·架构·开源协议
爱上纯净的蓝天1 天前
不用云做 RAG:一个本地笔记问答工具的取舍与坑
python·开源·大模型·rag·本地部署
电子云与长程纠缠1 天前
UE5魂系游戏开发教程05
开发语言·学习·游戏·ui·ue5