爬虫学习前记----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博客

相关推荐
oennn欧冷12 分钟前
中文关键字检索分析-导出到csv或者excel-多文件或文件夹-使用python和asyncio和pandas的dataframe
python·pandas·vba·asyncio·dataframe·completablefuture
小言从不摸鱼18 分钟前
【NLP自然语言处理】文本处理的基本方法
人工智能·python·自然语言处理
hummhumm26 分钟前
数据库系统 第46节 数据库版本控制
java·javascript·数据库·python·sql·json·database
ac-er888837 分钟前
Flask如何创建并运行数据库迁移
数据库·python·flask
日记成书37 分钟前
【无线通信发展史⑨】1791年路易吉·伽伐尼-关于动物电的研究与1800年亚历山大·伏打伯爵-电池:伏打电池
网络·人工智能·学习·职场和发展·信息与通信
贾saisai1 小时前
Xilinx系FPGA学习笔记(四)VIO、ISSP(Altera)及串口学习
笔记·学习·fpga开发
月夕花晨3741 小时前
C++学习笔记(13)
c++·笔记·学习
有点。1 小时前
Python系统教程004(字符串)
python
FreakStudio1 小时前
全网最适合入门的面向对象编程教程:46 Python函数方法与接口-函数与事件驱动框架
python·嵌入式·面向对象·电子diy
probably1211 小时前
学习记录之Java学习笔记3
java·笔记·学习