当前位置: 代码迷 >> Iphone >> 在公司写得一个监测iphone4供货情况的程序,上班了回家继续写
  详细解决方案

在公司写得一个监测iphone4供货情况的程序,上班了回家继续写

热度:61   发布时间:2016-04-25 06:12:18.0
在公司写得一个监测iphone4供货情况的程序,下班了回家继续写
package com.myapp.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;


import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class ReadWebPage {

/**
* @param args
* @throws IOException
* @throws InterruptedException
*/

// TODO Auto-generated method stub
public static void main(String[] args) throws IOException, InterruptedException {
MyThread  myThread = new MyThread();
myThread.start();
}

private static void writeToTmpFile(String str,File f) throws IOException {

FileWriter fw = new FileWriter(f,true);
fw.write(str+"\r\n");
fw.flush();
fw.close();
}

private static void getIphone4MarketInfo(File file){
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
// NodeList nodeList = document.getElementsByTagName("book");
//   for(int i=0;i<nodeList.getLength();i++){
// System.out.println(document.getElementsByTagName("title").item(i).getFirstChild().getNodeValue());
// System.out.println(document.getElementsByTagName("price").item(i).getFirstChild().getNodeValue());
//   }

  XPathFactory xpathFactory =XPathFactory.newInstance();
  XPath xpath = xpathFactory.newXPath();
  XPathExpression expression = xpath.compile("span[@class='customer_commit_display']/text()");
  Object result = expression.evaluate(document,XPathConstants.NODESET);
  NodeList nodeList = (NodeList)result;
  for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getNodeValue());
}

} catch (Exception e) {
e.printStackTrace();
}
}

public static String getTextFornHtml(String regEx,String inputText){
Pattern p;
Matcher m;
p=Pattern.compile(regEx);
m=p.matcher(inputText);
if(m.find()){
return m.replaceAll("");
}else{
return null;
}
}

public static boolean HtmlToTextGb2312(String inputString)
    {
              String htmlStr = inputString; //含html标签的字符串
              String textStr ="";
              Pattern p_script;
              Matcher m_script;
              Pattern p_style;
              Matcher m_style;
              Pattern p_html;
              Matcher m_html;
              Pattern p_houhtml;
              Matcher m_houhtml;
              Pattern p_spe;
              Matcher m_spe;
              Pattern p_blank;
              Matcher m_blank;
              Pattern p_table;
              Matcher m_table;
              Pattern p_enter;
              Matcher m_enter;
             
              Pattern p_iphone4;
              Matcher m_iphone4;
              boolean isFind=false;
              try {
               String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
               //定义script的正则表达式.
               String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
               //定义style的正则表达式.
               String regEx_html = "<[^>]+>";
               //定义HTML标签的正则表达式
               String regEx_houhtml = "/[^>]+>";
               //定义HTML标签的正则表达式
               String regEx_spe="\\&[^;]+;";
               //定义特殊符号的正则表达式
               String regEx_blank=" +";
               //定义多个空格的正则表达式
               String regEx_table="\t+";
               //定义多个制表符的正则表达式
               String regEx_enter="\n+";
               //定义多个回车的正则表达式
              
               String regEx_iphone4 ="<img[\\s\\S]*?alt=\"iPhone 4\">";
               p_iphone4 = Pattern.compile(regEx_iphone4,Pattern.CASE_INSENSITIVE);
               m_iphone4 = p_iphone4.matcher(htmlStr);
              
              
               p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
               m_script = p_script.matcher(htmlStr);
               htmlStr = m_script.replaceAll(""); //过滤script标签

               p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
               m_style = p_style.matcher(htmlStr);
               htmlStr = m_style.replaceAll(""); //过滤style标签
             
               p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
               m_html = p_html.matcher(htmlStr);
               htmlStr = m_html.replaceAll(""); //过滤html标签
              
               p_houhtml = Pattern.compile(regEx_houhtml,Pattern.CASE_INSENSITIVE);
               m_houhtml = p_houhtml.matcher(htmlStr);
               htmlStr = m_houhtml.replaceAll(""); //过滤html标签
              
               p_spe = Pattern.compile(regEx_spe,Pattern.CASE_INSENSITIVE);
               m_spe = p_spe.matcher(htmlStr);
               htmlStr = m_spe.replaceAll(""); //过滤特殊符号
              
               p_blank = Pattern.compile(regEx_blank,Pattern.CASE_INSENSITIVE);
               m_blank = p_blank.matcher(htmlStr);
               htmlStr = m_blank.replaceAll(" "); //过滤过多的空格
              
               p_table = Pattern.compile(regEx_table,Pattern.CASE_INSENSITIVE);
               m_table = p_table.matcher(htmlStr);
               htmlStr = m_table.replaceAll(" "); //过滤过多的制表符
              
                  
               p_enter = Pattern.compile(regEx_enter,Pattern.CASE_INSENSITIVE);
               m_enter = p_enter.matcher(htmlStr);
               htmlStr = m_enter.replaceAll(" "); //过滤过多的制表符
              
               textStr = htmlStr;
              isFind=m_iphone4.find();
              }catch(Exception e)
              {
                    System.err.println("Html2Text: " + e.getMessage());
              }
          
              //return textStr;//返回文本字符串
              return isFind;
    }
static class MyThread extends Thread{
public void run(){
while(true){
InputStream is = null;
try {
URL url = new URL(
"http://store.apple.com/cn/browse/home/shop_iphone/family/iphone");
InetSocketAddress address = new InetSocketAddress("10.17.171.10",
8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection(proxy);
connection.setConnectTimeout(60*1000);
is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = "";
File f = new File("d:\\sss.txt");
String outStr=null;
String text=null;
while ((line = br.readLine()) != null) {
text=getTextFornHtml("<img[\\s\\S]*?alt=\"iPhone 3GS\">",line);
if(text!=null){
text="iPhone 3GS:";
outStr=text;
}
text=getTextFornHtml("<img[\\s\\S]*?alt=\"iPhone 4\">",line);
if(text!=null){
text="iphone4:";
outStr+=text;
}
if(line.contains("product-specs")||line.contains("16 GB")||line.contains("customer_commit_display")
||line.contains("freeshipping")||line.contains("32 GB")||line.contains("8 GB")){
outStr += line+"\r\n";
}
}
text=getTextFornHtml("(<[^>]+>)||(/[^>]+>||\\t+||\\s+)", outStr);
text=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n"+text;
text=text+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n\r\n";
if(text.indexOf("24")!=text.lastIndexOf("24")){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date date= new Date();
String currentDate = sdf.format(date);
text="有货啦:"+currentDate+"\r\n"+text;
writeToTmpFile(text,f);
}
System.out.println(text.trim());
System.out.println("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
//getIphone4MarketInfo(f);
br.close();
Thread.sleep(10 * 1000L);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// public static void sendMail(String to, String toName,String subject, String body  ) {
//   try {
//    Properties prop = System.getProperties();
////    prop.put( "mail.smtp.auth ", "true ");
////    prop.setProperty("proxySet", "true");
//// //设置http访问要使用的代理服务器的地址   
////    prop.setProperty("socksProxyHost", "10.17.171.10");   
//    //设置http访问要使用的代理服务器的端口   
////    prop.setProperty("socksProxyPort", "8080");   
//    HtmlEmail email = new HtmlEmail();
//    email.setHostName("localhost");//设置发信的smtp服务器
//    email.addTo(to, toName);//设置收件人帐号和收件人
//    email.setFrom("vbtboy@126.com", "aaa");//设置发信的邮件帐号和发信人
//    email.setSubject(subject);//设置邮件主题
//    email.setAuthentication("aaa","111111");//如果smtp服务器需要认证的话,在这里设置帐号、密码
//    email.setHtmlMsg(body);//设置邮件正文和字符编码
//    email.send();
//   } catch (EmailException e) {
//    e.printStackTrace();
//   }
}