Stock Exchange Of Thailand [perl]

posted on 26 Jun 2013 09:00 by s4m3l0 in Code directory Tech, Diary, Idea
#!/usr/bin/perl -w
use LWP;
my $percen;
my $ua = LWP::UserAgent->new (
        agent => 'Mozilla/5.0 (X11; Linux i686; rv:5.0.1) Gecko/20100101 Firefox/5.0.1',
        timeout => 40,
		max_redirect => 0
);
my $c;
open ($c, ">>setthai.txt");
print $c "หุ้น\tล่าสุด\tเปลี่ยนแปลง\t%เปลี่ยนแปลง\n";
close($c);
my @symbol = ("A","AAV","ABICO","ADVANC","AEONTS","AFC","AH","AHC","AI","AIT","AJ","AJP","AKR","ALUCON","AMANAH","AMARIN","AMATA","AMC","ANAN","AOT","AP","APCS","APURE","APX","AQUA","AS","ASCON","ASIA","ASIAN","ASIMAR","ASK","ASP","AYUD","BAFS","BANPU","BAT-3K","BAY","BBL","BCH","BCP","BEAUTY","BEC","BECL","BFIT","BGH","BH","BIGC","BJC","BKI","BKKCP","BLA","BLAND","BLISS","BMCL","BNC","BRC","BROCK","BSBM","BTC","BTNC","BTS","BUI","BWG","CAWOW","CCET","CCP","CEI","CEN","CENTEL","CFRESH","CGS","CHARAN","CHOTI","CI","CIMBT","CIRKIT","CITY","CK","CM","CMR","CNS","CNT","CPALL","CPF","CPH","CPI","CPICO","CPL","CPN","CPNCG","CPNRF","CSC","CSL","CSP","CSR","CTARAF","CTW","CWT","DCC","DCON","DELTA","DEMCO","D-MARK","DRACO","DRT","DSGT","DTAC","DTC","DTCI","DTCPF","DTM","EASON","EASTW","ECL","EE","EGCO","EIC","EMC","EPCO","ERW","ESSO","ESTAR","EVER","F&D","FANCY","FE","FMT","FNS","FORTH","FSS","FUTUREPF","GBX","GC","GEN","GENCO","GFPT","GJS","GL","GLAND","GLOBAL","GLOW","GOLD","GOLDPF","GRAMMY","GRAND","GSTEL","GUNKUL","GYT","HANA","HEMRAJ","HFT","HMPRO","HTC","ICC","IEC","IFEC","IFS","IHL","INET","INOX","INSURE","INTUCH","IRC","IRPC","IT","ITD","ITV","IVL","JAS","JCP","JCT","JMART","JMT","JTS","JUTHA","KAMART","KBANK","KBS","KC","KCAR","KCE","KDH","KGI","KK","KKC","KMC","KSL","KTB","KTC","KTECH","KTP","KWC","KWH","KYE","LALIN","LANNA","LEE","LH","LHBANK","LHK","LHPF","LIVE","LL","LOXLEY","LPN","LRH","LST","LTX","LUXF","M-AAA","MAJOR","MAKRO","MALEE","MANRIN","MATCH","MATI","MAX","MBK","MBKET","M-CHAI","MCOT","MCS","MDX","METCO","METRO","MFC","MFEC","MIDA","M-II","MILL","MINT","MIPF","MJD","MJLF","MK","ML","MLINK","MME","MNIT","MNIT2","MNRF","MODERN","MONTRI","MPIC","MSC","MSPF","M-STOR","MTI","NC","NCH","NEP","NEW","NFC","NIPPON","NKI","NMG","NNCL","NOBLE","N-PARK","NSI","NTV","NUSA","NWR","OCC","OFM","OGC","OHTL","OISHI","OSK","PAE","PAF","PAP","PATKL","PATO","PB","PDI","PE","PERM","PF","P-FCB","PG","PICNI","PL","PLE","PM","POMPUI","POPF","POST","PPC","PPP","PR","PRANDA","PREB","PRECHA","PRG","PRIN","PRO","PS","PSAAP","PSL","PT","PTL","PTT","PTTEP","PTTGC","Q-CON","QH","QHHR","QHOP","QHPF","RAM","RASA","RATCH","RCI","RCL","RICH","RML","ROBINS","ROCK","ROH","ROJNA","RPC","RS","S+%26+J","SABINA","SAFARI","SAM","SAMART","SAMCO","SAMTEL","SAT","SAUCE","SAWANG","SC","SCAN","SCB","SCBLIF","SCC","SCCC","SCG","SCP","SCSMG","SEAFCO","SECC","SE-ED","SENA","SF","SFP","SGF","SGP","SHANG","SIAM","SIM","SINGER","SINGHA","SIRI","SIS","SITHAI","SKR","SMC","SMIT","SMK","SMM","SMPC","SMT","SNC","SNP","SOLAR","SORKON","SPACK","SPALI","SPC","SPCG","SPF","SPG","SPI","SPORT","SPPT","SRICHA","SSC","SSF","SSI","SSPF","SSSC","SST","SSTPF","STA","STANLY","STEC","STHAI","STPI","SUC","SUN","SUPER","SUSCO","SVH","SVI","SVOA","SYMC","SYNEX","SYNTEC","TASCO","TBSP","TC","TCAP","TCB","TCC","TCCC","TCIF","TCJ","TCMC","TCOAT","TCP","TEAM","TF","TFD","TFI","TFUND","TGCI","TGPRO","TH","THAI","THANI","THCOM","THIP","THL","THRE","TIC","TICON","TIF1","TIP","TIPCO","TISCO","TIW","TK","TKS","TKT","TLGF","TLOGIS","TLUXE","TMB","TMD","TMT","TNITY","TNL","TNPC","TNPF","TOG","TOP","TOPP","TPA","TPC","TPCORP","TPIPL","TPOLY","TPP","TPROP","TR","TRIF","TRS","TRU","TRUBB","TRUE","TSC","TSI","TSTE","TSTH","TT&T","TTA","TTCL","TTI","TTL","TTLPF","TTTM","TTW","TUCC","TUF","TU-PF","TVI","TVO","TWFP","TWP","TWZ","TYCN","TYM","UMI","UNIQ","UOB8TF","UOBAPF","UOBKH","UP","UPF","UPOIC","URBNPF","US","USC","UT","UTP","UV","UVAN","VARO","VGI","VGM","VIBHA","VIH","VNG","VNT","WACOAL","WAVE","WG","WHA","WHAPF","WIN","WORK","WR","YCI","YNP","ZMICO");
	foreach my $sym (@symbol) {
	print $sym,"\n";
	my $res = $ua->get('http://marketdata.set.or.th/mkt/stockquotation.do?symbol='.$sym.'&language=th&country=TH');
	$con = $res->content();
	$con =~ s/\n|\r|\s//g;
	if ($con =~ m/.+?<\/td>(.+?)<\/strong><\/font><\/td>/) {
		my $last = $1;
		next loop if ($last < 5);
		open ($c, ">>setthai.txt");
		print $c "$sym\t$last\t";
		close($c);
		if ($con =~ m/.+?<\/td>(.+?)<\/font><\/font><\/td>/) {
			my $change = $1;
			open ($c, ">>setthai.txt");
			print $c "$change\t";
			close($c);
		}
		if ($con =~ m/\\%.+?\<\/td\>\\\(.+?)\<\/font\>\<\/td>/) {
			$percen = $1;
			open ($c, ">>setthai.txt");
			print $c "$percen\n";
			close($c);
		}
	} elsif ($con =~ m/.+?<\/td>(.+?)<\/strong><\/font><\/td>/) {
		my $last = $1;
		open ($c, ">>setthai.txt");
		print $c "$sym\t$last\t"."-"."\t"."-"."\n";
		close($c);
	}
}
 
เป็น script เอาไว้เก็บข้อมูลราคาหุ้นทุกตัว ใน http://www.set.or.th ตัวอย่างที่ได้จะประมาณนี้คับ http://pastebin.com/uSsauUKc
 
ถ้าชื่อหุ้นตัวไหนที่เข้ามาใหม่ก็สามารถเพิ่มชื่อหุ้นได้ที่ตัวแปร @symbol คับ

edit @ 26 Jun 2013 09:22:07 by s4m3l0

Comment

Comment:

Tweet