1.1 Python与PyChar安装
1.1.1 Python安装
官方网站:https://www.python.org/
安装好后,在程序里可以找到,一个叫做python,一个叫做IDLE。
那种在cmd里敲python的方法,好像不管用
linux内置python么?
python -version #checkthe python3 version
sudo apt-get install python3.5 #install the python
1.1.2 PyCharm安装
IDE集成了代码编写,分析,编译,调试。
官网: http://www.jetbrains.com/pycharm/
PyCharm与Python解释器关联,
- 打开PyCharm—菜单栏----file----defalut settings命令
- 对话框—project interpreter----python环境(右)----python3.5----ok
1.2 变量与字符串
1.2.1 变量
变量赋值例子,完成互换
a = 4
b = 5
t = a
a = b
b = t
print(a,b)
#result 5 4
1.2.2 字符串的‘加法’和‘乘法’
字符串相加
字符串由双引号,或者单引号,和引号中的字符组成。
爬虫中经常构造URL。爬取一个网页链接经常有部分链接需要嫁接。
字符串加法
a = 'I'
b = 'love'
c = 'Python'
print(a+b+c)
# result I love Python
字符串乘法
a = 'word'
print(a*3)
#result wordwordword
1.2.3 字符串的切片和索引
爬虫实战中,经常会用到。
a = 'i love python'
print(a[0])
# result i
print(a[0:5])
#result i lov
print(a[-1])
#result n
1.2.4 字符串方法
- split()方法
通过给定的分隔符,将一个字符串分割为一个列表
如果没有提供任何分隔符,程序会把所有的空格作为分隔符号。(空格,制表,换行)
a = 'www.baidu.com'
print(a.split('.'))
['www', 'baidu', 'com']
- repalce()方法
这种方法类似文本中的“查找和替换”功能
>>> a = 'there is apples'
>>> b = a.replace('is','are')
>>> print(b)
there are apples
- strip()方法
返回去除两侧(不包括内部)空格的字符串,也可以指定需要去除的字符。
只能去除两侧。
>>> a = ' python is cool '
>>> print(a.strip())
python is cool
>>> a = '****python is good****'
>>>> print(a.strip('*!'))
python is good
- format()方法
爬虫过程中,有些网页链接的部分参数是可变的,使用格式化字符可以减少代码使用量。
>>> a = '{} is my love'.format('python')
>>> print(a)
python is my love
>>>
##下面的例子还没有实验过。
content = input('请输入搜索内容')
url_path = 'https://www.pexels.com/search/{}/'.format(content)
print(url_path)
1.3 函数和控制语句
1.3.1 函数
def 函数名(参数1,参数2,参数3):
return ‘结果’
def function(a,b)
return '1/2*a*b'
def change_number(number):
hiding_number = number.replace(number[3:7],'*'*4)
print (hiding_number)
change_number('1234579854687')
1.3.2 判断语句
if confition:
do
else:
do
if confition:
do
elif condition:
do
else:
do
输入密码正确即可登录,
def count_login():
password = input('password')
if password == '12345':
print('Sucessess!')
else:
print('wrong,again')
count_login()
count_login()
1.3.3 循环语句
#for循环
for item in iterable:
do
#iterm 表示元素,iterable 是集合。
for i in range(1,11):
print(i)
#while循环
while condiftion
do
i = 0
sum = 0
while i < 100:
i = i+ 1
sum = sum + i
print(sum)
1.4 Python数据结构
1.4.1 列表
列表中元素可变,有序,可容纳所有对象。
list = ['peter','lilei','batianhu']
print (list[0])
print (list[2:])
list = [
1,
1.1,
'string',
print(1),
True,
[1,2],
(1,2),
{'key','value'}
]
names = ['xiaoming','xiaoqiang','peter']
ages = [23,15,58]
for name, age in zip(names,ages):
print(name,age)
构造url
urls = ['http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(number)
for number in range(1,14)]
for url in urls:
print(url)
1.4.2 字典
插入MongoDB数据库需要字典结构。
字典的创造。
user_info = {
'name','xiaoming',
'age','14',
'sex','man'
}
1.4.3 元组和集合
爬虫中很少用到,元组只能查看不能修改。
tuple = (1,2,3)
集合里无序,但是无重复对象,所以可以用集合把重复数据删除
集合使用大括号构建的。
list = ['xm','zz','xm']
set = set(list)
print(set)
result: {'zz', 'xm'}
1.5 Python文件操作
语法:
open (name[,mode[,buffering]])
文件名作为唯一强制参数,返回一个文件对象。 mode ,buffering可选。
文件操作中,mode有必要,buffering使用较少。
f = open('d:/23Python/0926test7.txt')
#测试了下没反应
mode 参数值
‘r’ 读模式
‘w’ 写模式
‘a’ 追加模式
‘b’ 二进制模式(可添加到其他模式中使用)
‘+’ 读/写模式(可添加到其他模式中使用)
1.5.2 读写文件
没有创建文件也会自动创建文件。
但是这个例子 无法写入,是什么原因?
f = open('d:/23Python/0928test8.txt','w+')
f.write('hello world!')
w+ 只能写一次
r+ 可以一直写,可是仍然不出效果
f = open('d:/23Python/0928test8.txt','r+')
f.write('hello world!')
读取文件内容,。
f = open('d:/23Python/0928test8.txt','r')
content = f.read()
print(content)
1,文件打开要关闭,
2,r+可以继续写,从第一行开始,不是从后面附加,这个后续看看如何跟着后面写
3,中间写完加了个close再读才能读出来。这个什么逻辑?
f = open('d:/23Python/0928test8.txt','r+')
f.write('hello world')
f.close()
f = open('d:/23Python/0928test8.txt','r')
content = f.read()
print(content)
f.close()
1.6 Python面向对象
1.6.1 定义类
#定义类
class Bike:
compose = ['frame','wheel','pedal']
# 类的实例化
my_bike = Bike()
you_bike = Bike()
print(my_bike.compose)
print(you_bike.compose)
1.6.2 实例的属性
class Bike:
compose = ['frame','wheel','pedal']
my_bike = Bike()
my_bike.other = 'basket'
print(my_bike.other)
通过给类的实例属性赋值,compose属性属于所有的该款自行车,而other属性只属于my_bike这个类的实例。
1.6.3 实例方法
class Bike:
compose = ['frame','wheel','pedal']
def use(self):
print('you are ridding')
my_bike = Bike()
my_bike.use()
感觉可以这样理解,一个类Bike, 参数是compose,还可以定义函数,来执行动作。 然后一个my_bike是类的实例化。 然后my_bike.use()就是这个实例的具体动作的输出。 self 是实例本身。
实例方法也可以带参数,那么这个参数可不可以用手动输入呢?
class Bike:
compose = ['frame','wheel','pedal']
def use(self,time):
print('you ride {}m'.format(time*100))
my_bike = Bike()
my_bike.use(10)
魔术方法沙呀,创建实例的时候,不需要引用也会被执行。
其实没有太明白。
class Bike:
compose = ['frame','wheel','pedal']
def __init__(self):
self.other = 'basket'
def use(self,time):
print('you ride {}m'.format(time*10))
my_bike = Bike()
print(my_bike.other)
Traceback (most recent call last):
File “D:/23Python/092614.py”, line 8, in
print(my_bike.other)
AttributeError: ‘Bike’ object has no attribute ‘other’
1.6.4 类的继承
class Bike:
compose = ['frame','wheel','padeal']
def __init__(self):
self.other = 'basket'
def use(self,time):
print('your ride {}m'.format(time*100))
class Share_bike(Bike):
def cost(self,hour):
print('you spend {}'.format(hour*2))
bike = Share_bike()
print(bike.other)
bike.cost(2)