爬虫学习前记----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博客](https://blog.csdn.net/a5225354/article/details/106356770 "Python之unittest框架的介绍及使用_python unittest abc-CSDN博客")

相关推荐
qq_3863226942 分钟前
华为网路设备学习-21 IGP路由专题-路由过滤(filter-policy)
前端·网络·学习
J先生x1 小时前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
码上淘金4 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo4 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
2301_787552875 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
虾球xz5 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
懵逼的小黑子5 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
Y3174295 小时前
Python Day23 学习
python·学习
Ai尚研修-贾莲6 小时前
Python语言在地球科学交叉领域中的应用——从数据可视化到常见数据分析方法的使用【实例操作】
python·信息可视化·数据分析·地球科学
song_ly0016 小时前
深入理解软件测试覆盖率:从概念到实践
笔记·学习·测试