# -*- coding = ut-8 -*-# @Time : 2021/5/20 下午 13:14# @Author : PHC# @File demo1.py# @Software : PyCharm# 每查看一个详情信息 需要消耗100积分#若要在其他的工程文件中,需要在File->setting->python Interpreter 中重新下载导入的模块包import requestsimport urllibimport jsonimport xlwtimport time#打印爬取到网页的全部信息 全局变量dataListOne=[] #按页数,数组长度为所查的页数。def allUrl(current): # pageNum(current) baseurl = 'https://t.cjcyw.com/cjcy/ship/list?_index={}'.format(current) #地址拼接。当前访问的指定页数 # print(current) #输出的结果为1 #不同的设备head 中的信息不一致,后续需要更改。 head = { "Cookie": "cjcy_session_id=a7ff8f24-fcb2-43d3-80e4-53a7d466cea9; yunsuo_session_verify=6a7ff9e97531e25af61b0eeef6212804", # 'Accept': 'application/json', 'User-Agent': 'cicy/5.4(iPhone; iOS 12.0; Scale/3.00)', # 'Accept-Language':"zh-Hans-CN;q=1,da-DK;q=0.9", # "Content-Length": 62, "Content-Type": "application/x-www-form-urlencoded", "Connection": "keep-alive", "Host": "t.cjcyw.com" } request = urllib.request.Request(baseurl, headers=head) response = urllib.request.urlopen(request) page = response.read().decode("utf-8") #输出为str # print(page) contents=json.loads(page) #转为python对象。 # print(contents,'contents的结果是什么11111111111') content=contents['data']['records'] #每一页中的所有数据 # print(content,'content的结果是什么222222222') #[ {1},{2},{3}.... ] # print(type(content),'content的类型是什么') #输出结果是 <class 'list'> datalist = [] for i in content: #遍历每一页中的每一条数据,一页共20条数据。 # print(i) data=[] #将每条船的信息加入到data列表中 # print(i,'i的结果是什么') #将所需页面数的records中的数据提取。 startLocation=i['suozaidi'] #打印每一条信息中需要提取的信息------1所在地 # print(startLocation) data.append(startLocation) endLocation=i['daodadi'] # 2到达地 data.append(endLocation) shipName=i['title'] data.append(shipName) # 3船名 dunwei=i['dunwei'] # 4吨位 # print(dunwei) data.append(dunwei) # datalist.append(data) # print(data) #将每一个船的信息,形成一个列表。 #每一条船第一次普通信息的加载 aidFirst = i['aid'] # 下一个详情所需的参数接口 所有接口的参数均为 aid contentsOne=oneShipMessage(aidFirst) #打印所查看的一只船的信息。 # print(type(contentOne),'contentOne 的类型是什么') #输出结果是: <class 'dict'> 是列表类型。 # print(contentsOne,'contentOne的结果') # 打印单个船舶第一次加载的信息 aid=contentsOne['aid'] #下一次请求时,所需的参数。 应该与上一个aid相等。便于验证 # data.append(aid) oneShipLocation=contentsOne['suozaidi'] # 验证第二次加载得信息,所在地与第一次是否相同 # print(oneShipLocation) # data.append(oneShipLocation) starDate=contentsOne['kaishiriqi'] # 5 提取到的开始日期还未转换,当前显示的形式是从1970,01 到所爬取出的时间的秒数。 currentDate=time.strftime("%Y-%m-%d",time.localtime(starDate)) #经过标准格式化输出的时间 data.append(currentDate) #每一条船第二次查看 所加载的会员信息 contentTwo=twoVipAll(aidFirst) beizhu=contentTwo['beizhu11'] data.append(beizhu) # 6 备注的信息 LinkName=contentTwo['publishLinkMan'] data.append(LinkName) #7 联系人名字 linkPhone=contentTwo['publishTel'] data.append(linkPhone) #8 联系人电话 # datalistOne datalist datalist.append(data) # 注:[ [ [data],[data],[data] ] , [ [data],[data],[data] ] , .. ] print("第",current,"页,第",len(datalist),"条",data) #查看每打印一条船舶信息的进度 dataListOne.append(datalist) #将第一次加载出来的信息,加入列表中 # print(dataListOne) return dataListOne#需要打印的页面条数数据# def pageNum(number):# for current in range(1,number): # 由于range()中是左闭右开,且allUrl(current)当中的current数值只能从一开始。故当pagenNum(4) 传过来的参数为4 时,只能打印3条。# allUrl(current)#第一次点击查看 查看一个船的普通信息def oneShipMessage(aid): url="https://t.cjcyw.com/cjcy/ship/detail?aid={}".format(aid) head = { "Cookie": "cjcy_session_id=a7ff8f24-fcb2-43d3-80e4-53a7d466cea9; yunsuo_session_verify=6a7ff9e97531e25af61b0eeef6212804", # 'Accept': 'application/json', 'User-Agent': 'cicy/5.4(iPhone; iOS 12.0; Scale/3.00)', # 'Accept-Language':"zh-Hans-CN;q=1,da-DK;q=0.9", # "Content-Length": 62, "Content-Type": "application/x-www-form-urlencoded", "Connection": "keep-alive", "Host": "t.cjcyw.com" } request = urllib.request.Request(url, headers=head) response = urllib.request.urlopen(request) pageOne= response.read().decode("utf-8") # print(type(pageOne),"pageOne的类型是什么") #输出结果 <class 'str'> contents=json.loads(pageOne) # print(contents,'contents的结果333333333') # 输出结果正确 content=contents['data'] # print(content,'content 的结果444444444') # { } # print(type(content)) #输出结果为 <class 'dict'> 已经转成dict列表形式,应该转成list。 return content # 返回第一次加载后的信息# 第二次查看中间过度段的请求过程。需要积分,会员形式查看def twoVipCenter(aid): #需要传过来一个参数,名为aid #中间有一个过度请求的地址信息 #过度请求 url="https://t.cjcyw.com/cjcy/user/viewDetail?aid={}".format(aid) head={ "Cookie": "cjcy_session_id=a7ff8f24-fcb2-43d3-80e4-53a7d466cea9", "Host": "t.cjcyw.com", "User-Agent": "cicy/5.4 (iPhone; iOS 12.0; Scale/3.00)", "Content-Type": "application/x-www-form-urlencoded" } request=urllib.request.Request(url,headers=head) response=urllib.request.urlopen(request) pagetwo=response.read().decode("utf-8") contents=json.loads(pagetwo) # print(contents) # 成功 return contents#第二次所需要查看的详情信息。def twoVipAll(aid): #中间过渡段返回过来的内容: contentVipCenter=twoVipCenter(aid) message=contentVipCenter['message'] # 'message':'成功'--------输出结果成功,能够正常度过中间访问过程。 **********(意味这一段没毛病) # print(message) url="https://t.cjcyw.com/cjcy/ship/detail?aid={}".format(aid) head={ "Cookie": "cjcy_session_id=a7ff8f24-fcb2-43d3-80e4-53a7d466cea9", "Host": "t.cjcyw.com", "User-Agent": "cicy/5.4 (iPhone; iOS 12.0; Scale/3.00)", "Content-Type": "application/x-www-form-urlencoded" } request=urllib.request.Request(url,headers=head) response=urllib.request.urlopen(request) pagetwoAll=response.read().decode("utf-8") contents=json.loads(pagetwoAll) content=contents['data'] # print(content,'第二次查看时的contents的内容') return content#保存所有的信息到Excel表格中。def saveMessagePath(number): for current in range(1,number): #左闭右开 [a,b) # print(current) #输出结果是 1 2 datalist= allUrl(current) # print(datalist) timePass=time.localtime() #输出系统的当前时间,当前时间为格林尼治时间? 秒数, timeNow=time.strftime("%Y-%m-%d",timePass) #将系统的当前时间格式化输出,一天打印一次,若重复打印,最好将已输出的文件名字更改一下,避免数据丢失,重名会覆盖。 savePath="%s长江船运网数据信息.xls"%(timeNow) #保存的文件命名格式:当前时间(年-月-日)+文件名 table=xlwt.Workbook(encoding="utf-8",style_compression=0) sheet=table.add_sheet("长江船运网数据信息",cell_overwrite_ok=True) col=("所在地","到达地","船名","吨位","开始日期","备注信息","船主名字","电话") #定义列名 # print(datalist) #定义表格中的列名 for i in range(0,len(col)): sheet.write(0,i,col[i]) #向表格中写入列名 for pages in range(len(datalist)): #遍历每一页。总页数是datalist元组的长度 pagesData=datalist[pages] #取出每一页中的20条数据。 # print("看看有多少页====",pages) # 当输入的number页数为3时,打印两条数据。datalist长度为2。pages输出结果0,1 # print("看看第%s页有多少条船舶数据"%(pages+1)) #页数在列表中,下标从0开始计算 或(pages+1) if pages < number: for j in range(0,20): #遍历每一页中的20条数据 print("第%s条"%(j+1)) #该条程序已执行 item =pagesData[j] #取出每一条数据 # print(item) #输出结果是想要的形式。 for a in range(0,len(col)): sheet.write(j+pages*20+1,a,item[a]) #Excel中表格第一行,第一列坐标为(0,0) sheet.write(行,列,写入的数据) table.save(savePath)if __name__=='__main__': # pageNum(3) #页数更改处,从2开始 pagesNum=5 #定义所需要查找的页数。 print("当前需要查询的页数共有", pagesNum-1, "页,每页应当有20条,请耐心等待查询的结果\n结果加速查询中......") saveMessagePath(pagesNum) #输入口,定义所需要查看的页数。。20条信息为一页。 # twoVipCenter(6614081) #测试第二次查看中间过渡段能否成功访问。 # twoVipAll(6614081) #测试第二次查看所有信息时,能否访问成功