标签:sprintf print pen plain size visit 分析文件 下载 sni
平时上班比較忙,常常错过支持的球队的比赛。所以可以知道支持的球队本周都有哪些比赛将会是一个不错的选择,于是自己动手实现了这个想法。
初学Python,练习的缘故,部分功能使用python实现。
整体思想就是从指定站点下载下比赛信息,然后分析之。
C++部分,主要实现文件的下载:
#include <windows.h> #include <iostream> #include <URLMON.H> #include <FSTREAM> #include <TIME.H> using namespace std; #pragma comment(lib, "ws2_32.lib") #pragma comment(lib,"urlmon.lib") int main() { char likeTeam[1024]; ifstream fread( "likeTeamList.txt" ); if( !fread ) { cout<<"Can't open likeTeamList.txt!"<<endl; system( "PAUSE" ); return 0; } fread.getline( likeTeam, 1024, '\n' ); fread.close(); cout<<"赛事名称 开赛时间 主 队 客 队"<<endl; time_t tnow = time( NULL ); struct tm* tmNow = localtime( &tnow ); int wNow = tmNow->tm_wday; if( wNow == 0 ) wNow = 7; for( ; wNow != 8; wNow++ ) { char urlAddress[100]; sprintf( urlAddress, "http://www.uhchina.com/zuqiusaicheng/%dyue%dri.htm", tmNow->tm_mon + 1, tmNow->tm_mday ); char saveName[50]; sprintf( saveName, "%dyue%dri.txt", tmNow->tm_mon + 1, tmNow->tm_mday ); HRESULT hr = URLDownloadToFile( 0, urlAddress, saveName, 0, NULL ); if( hr != S_OK ) { cout<<"download url "<<urlAddress<<" failure!"<<endl; } else { char cCmd[1024]; sprintf( cCmd, "matchSchedule.py %s %s", saveName, likeTeam ); system( cCmd ); sprintf( cCmd, "del %s", saveName ); system( cCmd ); } tnow += 24 * 60 * 60; tmNow = localtime( &tnow ); } cout<<"赛程输出完成!"<<endl; getchar(); return 0; }
分析文件使用的是Python脚本----matchSchedule.py :
#coding: utf-8 import codecs import sys loveTeam = [] for i in range( 2, len( sys.argv ) ) : loveTeam.append( sys.argv[i] ) fread = codecs.open( sys.argv[1], "rU", "utf-8" ) n = 0 findNum = 0 for line in fread: n += 1 if n == 63 : break while 1: ltemp = fread.readline() templist = ltemp.split('>') if ltemp.find( 'onclick' ) == -1 : matchName = templist[2][0:templist[2].find('<')] matchTime = templist[5][0:templist[5].find('<')] else : matchName = templist[1][0:templist[1].find('<')] matchTime = templist[3][0:templist[3].find('<')] ltemp = fread.readline() templist = ltemp.split('>') if ltemp.find( 'class=\"gray\"' ) == -1 : homeName = templist[2][0:templist[2].find('<')] else : homeName = templist[4][0:templist[4].find('<')] ltemp = fread.readline() templist = ltemp.split('>') visiterName = templist[2][0:templist[2].find('<')] fread.readline() fread.readline() fread.readline() fread.readline() ltemp = fread.readline() for team in loveTeam : if team == homeName or team == visiterName : findNum += 1 print( '%-16s%-16s%-16s%-16s' % ( matchName, matchTime, homeName, visiterName ) ) if ltemp.find('个赛事共同拥有') != -1 or findNum == 4 : break fread.close()
喜爱的球队存储在likeTeamList.txt里,里面仅仅有一行,记录支持的球队。用空格隔开,形如:
阿森纳 拜仁 北京国安乐视 中国
输出结果:
标签:sprintf print pen plain size visit 分析文件 下载 sni
原文地址:http://www.cnblogs.com/gavanwanggw/p/7238882.html