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

相关推荐
阿福不是狗2 小时前
Python使用总结之Mac安装docker并配置wechaty
python·macos·docker
阿阳微客2 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
gen_2 小时前
mac环境下的python、pycharm和pip安装使用
python·macos·pycharm
AI视觉网奇2 小时前
pycharm 左右箭头 最近编辑
ide·python·pycharm
思绪无限2 小时前
Pycharm的终端无法使用Anaconda命令行问题详细解决教程
ide·python·pycharm·终端·命令行·anaconda·问题教程
漫步云端-r2 小时前
Pycharm的使用技巧总结
ide·python·pycharm
风逸hhh4 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮4 小时前
Python训练第四十三天
开发语言·python
互联网杂货铺5 小时前
完美搭建appium自动化环境
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
Gyoku Mint5 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib