python第二天

#python中的循环

#for循环

#格式:

#for临时变量in可迭代对象:

#循环体

name='neusoft'

for i in name:

    print(i)

#i为临时变量 不用提前声明 可自动创建

#range()可以写循环次数

#range(起始位置 终止位置 步长)可以写循环次数

#起始位置省略默认为0,步长为1

for i in range(1,2020):

    print(第,'i',次祝福新年快乐')

#生成一个[0,1,2,......20]的列表

#可使用循环创建

list1=[]

for i in range(21):

    list1.append(i)

    print(list1)

#Len()可以检测对象的元素个数

heroList=['卡莎','伊泽瑞尔','崔丝塔娜',100,]

print(heroList)

for i in range(len(heroList)):

    if heroList[i]=='艾希':

        print('恭喜你选中特殊英雄')

    else:

        print('不是特殊英雄')

#python制作进度条

#pip install tqdm

from tqdm import tqdm

import time

mylist=[]

for i in range(10):

    mylist.append(i)

#遍历mylist

for x in tqdm(mylist):

    time.sleep(1)

#strip 去空格操作

#name='  neudu  '

#print(len(name))

#name=name.strip()

#print(len(name))

#join将列表变成字符串

li=['shi','ma','ka','ze']

path=''.join(li)

print(path)

#获取网页源代码 requests

#pip install requests

#提取所需信息 xpath

import requests

response=requests.get('http://www.baidu.com')

#响应状态码

print(response.status_code)

#响应的编码方式

response.encoding='utf-8'

print(response.encoding)

#获取string响应类型

html_date=response.text

print(html_data)

#将获得的文件写成本地 html

with open('index.html','w',encoding='utf-8')as f:

    f.write(html_date)

#获取所需信息xpath

url='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577788919695&di=050608ac9b5b1e3921cda8ab06d78242&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20170918%2F25db737c935d48f8985735db4f46fded.jpeg'

reponses2=requests.get(url)

#获取byte类型的响应

response2=requests.content

with open('yuzhou.png','wb') as f:

    if reponse2.status_code == 200:

        f.write(img_data)

你可能感兴趣的:(python第二天)