python根据服务器sn号查询DELL服务器型号、出厂时间、过保时间

思路:根据请求url返回的html进行处理,获取对应的信息
相关url

1、自助查询
https://www.dell.com/support/home/cn/zh/cnbsd1/
2、指定sn号查询(GET请求)
https://www.dell.com/support/home/cn/zh/cnbsd1/product-support/servicetag/ 加上sn号
3、服务器详情页面(POST请求,需要提交"serviceTag"值,可从第二个url返回的html中获取)
https://www.dell.com/support/components/dashboard/cn/zh/cnbsd1/Warranty/GetWarrantyDetails

用法

D:盘根目录创建txt文档,名为sn.txt;存入svn清单,执行后会生成snlist文件,包含sn-型号-出厂-过保日期
其他自定义目录可自行修改

import requests,re,time
from requests.exceptions import RequestException
base_url='https://www.dell.com/support/home/cn/zh/cnbsd1/product-support/servicetag/'
def Get_Page(url):      #根据sn号获取返回页面
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None
def Get_Info(s_code):       #根据返回码,获取信息
    url_params = {"serviceTag":s_code}
    info_url='https://www.dell.com/support/components/dashboard/cn/zh/cnbsd1/Warranty/GetWarrantyDetails'
    r1=requests.post(info_url,data=url_params)
    h1=r1.text
    p_date=re.compile("<td>(\d+) (\d+)&#x6708; (\d+)</td>")
    p_in_date=re.compile('<td><b>(.*)(\d{2}) (\d+)&#x6708; (\d+)</b></td>')
    date2='error'
    if h1:
        c=h1.split("\n")
        for i in c:
            if '<td>NBD' in i:              #检测下一个工作日服务(优先级以上门服务器日期为准)
                n=c.index(i)
                # date1=p_date.findall(c[n+1])
                date2=p_date.findall(c[n+2])
            elif '%s</b></td>'%sn in i:
                n = c.index(i)
                date1=p_in_date.findall(c[n+1])
                # print(date1)
        if date2=='error':				#因为服务器型号不用,包含的服务不同,所以下面做了较多的判断
            for i in c:
                if '<td>POW' in i:          #检查部件保修服务
                    n = c.index(i)
                    date2 = p_date.findall(c[n + 2])
                elif '<td>4' in i:      #检查24小时上门
                    n = c.index(i)
                    date2 = p_date.findall(c[n + 2])
                if date2=='error':
                    date2=[('error','error','error')]   #返回错误数据
                in_date = ((date1[0][3] + '-' + date1[0][2] + '-' + date1[0][1]))  # 格式化日期
                out_date = ((date2[0][2] + '-' + date2[0][1] + '-' + date2[0][0]))
        else:
            in_date = ((date1[0][3] + '-' + date1[0][2] + '-' + date1[0][1]))  # 格式化日期
            out_date = ((date2[0][2] + '-' + date2[0][1] + '-' + date2[0][0]))

        return in_date,out_date
class DELLINFO():
    def __init__(self,sn):
        self.sn=sn

    def Get_Dellinfo(sn):  # 主程序
        html1 = Get_Page(base_url + sn)
        if html1:
            p1 = re.compile('servicetag/(.*)/overview"')
            p2 = re.compile('<h1 class="mb-3 mb-lg-1 text-center text-lg-left position-relative word-break">(.*)</h1>')
            r1 = p1.findall(html1)  # 过滤获取返回码
            r2 = p2.findall(html1)
            if r1 and len(r1) == 1:
                date = Get_Info(r1[0])
            if r2 and len(r2) == 1:
                server_mode = r2[0]
        return server_mode, date

with open('D:\sn.txt','r',encoding='utf-8') as f:
    for i in f:
        sn=i.strip()
        info=DELLINFO.Get_Dellinfo(sn)
        if len(info)==2:
            server_mode=info[0]
            server_in_date=info[1][0]
            server_out_date=info[1][1]
            info_detail=sn+'\t'+server_mode+'\t'+server_in_date+'\t'+server_out_date+'\n'
            print(server_mode)
            print(server_in_date)
            print(server_out_date)
            with open('D:snlist.txt','a',encoding='utf-8') as ff:
                ff.write(info_detail)

多线程版本

import requests,re,time
from requests.exceptions import RequestException
from concurrent.futures import ThreadPoolExecutor
import threading
base_url='https://www.dell.com/support/home/cn/zh/cnbsd1/product-support/servicetag/'
def Get_Page(url):      #根据sn号获取返回页面
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None

def Get_Info(s_code,sn):       #根据返回码,获取信息
    url_params = {"serviceTag":s_code}
    info_url='https://www.dell.com/support/components/dashboard/cn/zh/cnbsd1/Warranty/GetWarrantyDetails'
    r1=requests.post(info_url,data=url_params)
    h1=r1.text
    p_date=re.compile("<td>(\d+) (\d+)&#x6708; (\d+)</td>")
    p_in_date=re.compile('<td><b>(.*)(\d{2}) (\d+)&#x6708; (\d+)</b></td>')
    date2='error'
    if h1:
        c=h1.split("\n")
        for i in c:
            if '<td>NBD' in i:              #检测下一个工作日服务(优先级以上门服务器日期为准)
                n=c.index(i)
                # date1=p_date.findall(c[n+1])
                date2=p_date.findall(c[n+2])
            elif '%s</b></td>'%sn in i:
                n = c.index(i)
                date1=p_in_date.findall(c[n+1])
                # print(date1)
        if date2=='error':				#因为服务器型号不用,包含的服务不同,所以下面做了较多的判断
            for i in c:
                if '<td>POW' in i:          #检查部件保修服务
                    n = c.index(i)
                    date2 = p_date.findall(c[n + 2])
                elif '<td>4' in i:      #检查24小时上门
                    n = c.index(i)
                    date2 = p_date.findall(c[n + 2])
                if date2=='error':
                    date2=[('error','error','error')]   #返回错误数据
                in_date = ((date1[0][3] + '-' + date1[0][2] + '-' + date1[0][1]))  # 格式化日期
                out_date = ((date2[0][2] + '-' + date2[0][1] + '-' + date2[0][0]))
        else:
            in_date = ((date1[0][3] + '-' + date1[0][2] + '-' + date1[0][1]))  # 格式化日期
            out_date = ((date2[0][2] + '-' + date2[0][1] + '-' + date2[0][0]))

        return in_date,out_date
def write_data(server_mode,date,sn):
    server_mode = server_mode
    server_in_date = date[0]
    server_out_date = date[1]
    info_detail = sn + '\t' + server_mode + '\t' + server_in_date + '\t' + server_out_date + '\n'
    print(server_mode)
    print(server_in_date)
    print(server_out_date)
    with open('D:snlist.txt', 'a', encoding='utf-8') as ff:
        ff.write(info_detail)

class DELLINFO():
    def __init__(self,sn):
        self.sn=sn

    def Get_Dellinfo(sn):  # 主程序
        html1 = Get_Page(base_url + sn)
        if html1:
            p1 = re.compile('servicetag/(.*)/overview"')
            p2 = re.compile('<h1 class="mb-3 mb-lg-1 text-center text-lg-left position-relative word-break">(.*)</h1>')
            r1 = p1.findall(html1)  # 过滤获取返回码
            r2 = p2.findall(html1)
            if r1 and len(r1) == 1:
                date = Get_Info(r1[0],sn)
            if r2 and len(r2) == 1:
                server_mode = r2[0]
        write_data(server_mode,date,sn)

if __name__=='__main__':
    t_list = []
    sn_list=[]
    pool=ThreadPoolExecutor(10)
    # freeze_support()
    with open('D:\sn.txt','r',encoding='utf-8') as f:
        for i in f.readlines():
            sn_list.append(i.strip())
            t = threading.Thread(target=DELLINFO.Get_Dellinfo, args=(i.strip(),))
            t_list.append(t)
            t.start()
        for t in t_list:
            t.join()

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页