当前位置: 首页 > 后端技术 > Python

Python获取知投之星数据写入excel

时间:2023-03-26 00:30:14 Python

#-*-编码:utf-8-*-importrequestsimportsysimportreimportioimportxlwtimportxlrdimporttimesys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')defgetHtml(url):#userequests获取网页#Constructheaderheaders={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/79.0.3945.130Safari/537.36'}cook={'cookies':"****************"}time.sleep(0.2)gethtml=requests.get(url,headers=headers,cookies=cook)returngethtml.textdefbiaoName():#表名sheet.write(0,0,"编号")sheet.write(0,1,"基金名称")sheet.write(0,2,"日期")sheet.write(0,3,"操作")returnbiaoNameurl="http://www.zhitouxing.com/my_strg"resp=getHtml(url)FoundNames=re.findall("(.*?)",resp,re.S)#正则表达式FoundTimes=re.findall("(.*?)
",resp,re.S)FoundMakes=re.findall("今天预定投票(.*?)",resp,re.S)workbook=xlwt.Workbook(encoding="utf-8")sheet=workbook.add_sheet('智投星获取信息')biaoName()foriinrange(0,len(FoundNames),1):sheet.write(i+1,0,i+1)sheet.write(i+1,1,FoundNames[i])sheet.write(i+1,2,FoundTimes[i])sheet.write(i+1,3,FoundMakes[i])i=i+1工作簿。save(r"C:\Users\fuxingyu\Desktop\Zhitouxing.xls")代码打包成exepyinstaller-Fztx.py