Python代码练习<含Leetcode题库与算法练习>---持续更新

Python代码练习<含Leetcode题库与算法练习>---持续更新

#冒泡排序法
要点:python的列表元素替换 a,b = b,a

alist = [9,2,6,0]
def ascFunc(list):
    times = len(list)-1
    for i in range(times):
        for j in range(times-i):   #0--0,1,2   1--0,1
            if list[j] > list[j+1]:
                list[j],list[j+1] = list[j+1],list[j]
    return list
print(ascFunc(alist))

#面向对象入门练习
要点:
确定好类与实例;
后续操作的对象是实例,体现在房间存入的时候存的是实例。
在这里插入图片描述

#----------------定义类----------------------
class Tiger:
    nickname = '老虎'
    def __init__(self,inWeight):
        self.weight = inWeight
    def roar(self):
        print('Wow!!!')
        self.weight -= 5
    def feed(self,food):
        if food == 'meat':
            self.weight += 10
        else:
            self.weight -= 10

class Sheep:
    nickname = '羊'
    def __init__(self,inWeight):
        self.weight = inWeight
    def roar(self):
        print('Mie~~~')
        self.weight -= 5
    def feed(self,food):
        if food == 'grass':
            self.weight += 10
        else:
            self.weight -= 10

class Room:
    def __init__(self,inNum,inAnimal):
        self.num  = inNum
        self.animal = inAnimal

#----------------初始化----------------------
from random import randint
roomList = []   #存储房间实例用
for one in range(1,11):
    if randint(0,1) == 1:      #双闭区间
        ani = Tiger(200)       #要的是实例,因为这里老虎固定200,所以在类里面可以设置缺省参数:def __init__(self,inweight=200):
    else:
        ani = Sheep(100)
    room = Room(one,ani)  #这是房间实例,所以存储在列表里的元素不是字符串也不是int。。
    roomList.append(room)

#时间差
import  time
start = time.time()    #1970年到现在时间的秒数,取一次所以是记录的开始时间
while True:
    curTime = time.time()   #实时获取
    if curTime - start >= 30:
        for one in roomList:
            print(f'房间编号是:{one.num}  动物是:{one.animal.nickname}  体重是:{one.animal.weight}斤')
        break

#----------------游戏操作----------------多线程取做这个,单线程如果用户不输入就一直卡在input,多线程一个进行游戏,一个进行计时
    roomNum = randint(0,9)
    roomObject = roomList[roomNum]  #取出房间实例
    answer = input(f'当前房间编号:{roomNum},是否敲门(y/n)')  #提示用户操作,可以增加些输入错误判断进行优化
    if answer == 'y':
        #这个房间.动物.叫
        roomObject.animal.roar()
    food = input('请投喂食物:(meat/grass)')
    roomObject.animal.feed(food)

#简单爬虫练习,爬取51job职位信息
要点:
注意请求的SSL验证;
注意f12的文本信息,正则尽可能准确些;
新网页可以用方法查询request的猜测编码,确认真实编码后转码,很有用!!!在这里插入图片描述

user_header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}

'''
https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=
https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,2.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=
https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,3.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=
'''

import requests,re
import xlwt
requests.packages.urllib3.disable_warnings()   #移除验证SSL证书后,出现警告,简单点直接移除警告即可
web_url = 'https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare='

workbook = xlwt.Workbook(encoding='utf-8')
sheet1 = workbook.add_sheet('52jobs')class="td">(.*?)页,到第</sp

def getPages(url):
    res = requests.get(url,headers=user_header,verify=False)  #避免验证证书报错
    res.encoding='gbk'
    pages = re.findall('class="td">共(.*?)页,到第</sp',res.text,re.S)[0]  #res是返回码,至少要res.text;另外保险起见取列表第一个[0]
    return int(pages)

pagenum = getPages(web_url)

row = 1
for page in range(1,pagenum+1):
    cur_url = f'https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,{page}.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare='
    res = requests.get(cur_url,headers=user_header,verify=False)
    res.encoding='gbk'
    reslist = re.findall('<div class="el">(.*?)</div>',res.text,re.S)

    for i in reslist:
        jobname = re.findall('_blank" title="(.*?)" href',i,re.S)[0]
        sheet1.write(row,0,jobname)
        companyname = re.findall('_blank" title="(.*?)" href',i,re.S)[1]
        sheet1.write(row,1,companyname)
        address = re.findall('class="t3">(.*?)</span>',i,re.S)[0]
        sheet1.write(row,2,address)
        salary = re.findall('class="t4">(.*?)</span>',i,re.S)[0]
        sheet1.write(row,3,salary)
        update = re.findall('class="t5">(.*?)</span>',i,re.S)[0]
        sheet1.write(row,4,update)
        row += 1

workbook.save(r'/Users/jason/Downloads/API/52job.xls')

#最好单点调试,查看下res.text的返回,然后再正则,直接f12正则容易出错
'''
<div class="el">
        <p class="t1 ">
            <em class="check" name="delivery_em" οnclick="checkboxClick(this)"></em>
            <input class="checkbox" type="checkbox" name="delivery_jobid" value="123579108" jt="0" style="display:none">
            <span>
                <a target="_blank" title="自动化测试工程师" href="https://51rz.51job.com/job.html?jobid=123579108" οnmοusedοwn="jobview('123579108');">
                    自动化测试工程师                </a>
            </span>
                                                                    </p>
        <span class="t2"><a target="_blank" title="“前程无忧”51job.com(上海)" href="https://51rz.51job.com/job.html?jobid=71752221&amp;coid=1249">“前程无忧”51job.com(上海)</a></span>
        <span class="t3">上海-浦东新区</span>
        <span class="t4"></span>
        <span class="t5">07-12</span>
    </div>
'''

'''
<div class="dw_page">
    <div class="p_box">
        <div class="p_wp">
            <div class="p_in">
                <ul>
                                            <li class="bk"><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,2.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">上一页</a></li>
                                                                                            <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,1.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">1</a></li>
                                                                                                <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,2.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">2</a></li>
                                                                                                <li class="on">3</li>
                                                                                                <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,4.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">4</a></li>
                                                                                                <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,5.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">5</a></li>
                                                                                                <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,6.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">6</a></li>
                                                                                                <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,7.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">7</a></li>
                                                                                                <li><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,8.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">8</a></li>
                                            
                                            <li class="bk"><a href="https://search.51job.com/list/020000,000000,0000,00,9,99,%25E8%2587%25AA%25E5%258A%25A8%25E5%258C%2596%25E6%25B5%258B%25E8%25AF%2595,2,4.html?lang=c&amp;postchannel=0000&amp;workyear=99&amp;cotype=99&amp;degreefrom=99&amp;jobterm=99&amp;companysize=99&amp;ord_field=0&amp;dibiaoid=0&amp;line=&amp;welfare=">下一页</a></li>
                                    </ul>
                <input type="hidden" id="hidTotalPage" value="31">
                <span class="td">共31页,到第</span><input id="jump_page" class="mytxt" type="text" value="3"><span class="td">页</span><span class="og_but" οnclick="jumpPage('31');">确定</span>
            </div>
        </div>
    </div>
</div>
'''
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值