httpclient

package com.teamdev.jxbrowser.chromium.demo_lingshui.baidunuomi.shop;

import java.awt.BorderLayout;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hyjx.common.CommonUtil;
import com.hyjx.orclJdbcUtil.JDBCUtils;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
/**
* 百度糯米抓 评论总数 (COMM_TOTAL ) 差评数 (NEGATIVE_NUM) 店铺
*
*/
public class ls_bdnm_shop{
public static void main(String[] args) throws Exception {

    java.sql.Connection conOrcale  = null;
try {
conOrcale =    JDBCUtils.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = null;
PreparedStatement ps = null;
String  sql1 = null;
String  sql2 = null;
Statement st2 = null;
ResultSet rs2 = null;

Document doc = null;
try{
    sql1 = " select * from ls_nm_shop_good t  where t.plat_name ='百度糯米' and state is null ";
    sql2 = "update  ls_nm_shop_good set state = 'shop' , shop_url =? where good_url = ? ";
    st2  = (java.sql.Statement) conOrcale.createStatement();
rs2  = st2.executeQuery(sql1);
ps = conOrcale.prepareStatement(sql2);
}catch (Exception e) {
e.printStackTrace();
}
int i = 1;
String gurl="";
for(;rs2.next();){
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(i);
i++;
final  String good_url = rs2.getString("good_url");
   String good_type = rs2.getString("good_type");
try {
gurl= search(good_url,good_type);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("该商品已经下线或者该店铺没有url");
continue;
}
//修改状态
ps.setString(1,gurl);
ps.setString(2,good_url);
    ps.executeUpdate();
}

       
}

static String search(final String good_url ,String good_type)throws Exception{
System.out.println("url========="+good_url);
java.sql.Connection conOrcale2  = null;
try {
conOrcale2 =    JDBCUtils.getConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = null;
PreparedStatement ps = null;
try{
sql = "insert into ls_nm_shop   "+
"  (shop_id, site_ent_id, platform_code,shop_name,shop_address,shop_url,tellphone,type)  "+
"  values " +
"( ?   ,  ? ,   ?   ,  ?,  ? ,  ?,  ?,?)  ";
    ps = conOrcale2.prepareStatement(sql);
}catch (Exception e) {
e.printStackTrace();
}

Document doc = null;
String text ="";
//httpclient
CloseableHttpClient httpclient = HttpClients.createDefault(); 
HttpGet httpget = new HttpGet(good_url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String ahtml = EntityUtils.toString(entity, "UTF-8");
httpget.releaseConnection();


String deal_id="";
if(ahtml.contains("article")){
deal_id=ahtml.substring(ahtml.indexOf("article")+8,ahtml.indexOf("section"));
// F.context("goods_id", "31217598");
deal_id=deal_id.substring(deal_id.lastIndexOf("=")+1,deal_id.length()-5);
}
if(ahtml.contains("goods_id")&&deal_id ==null){
deal_id=ahtml.substring(ahtml.indexOf("goods_id")+8,ahtml.indexOf("merchant_ids"));
// F.context("goods_id", "31217598");
deal_id=deal_id.substring(deal_id.lastIndexOf(",")+3);
deal_id=deal_id.substring(0,deal_id.lastIndexOf(")")-1);
}

System.out.println("deal_id="+deal_id);
System.out.println("https://www.nuomi.com/pcindex/main/shopchain?dealId="+deal_id);
//详细
doc = Jsoup.connect("https://www.nuomi.com/pcindex/main/shopchain?dealId="+deal_id).userAgent("Mozilla")
.header("method", "GET")
.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
.header("Accept-Encoding:","gzip, deflate, sdch")
.header("Accept-Language","zh-CN,zh;q=0.8")
.header("Cache-Control","max-age=0")
.header("Connection","keep-alive")
.header("Host","lingshui.nuomi.com")
.header("Upgrade-Insecure-Requests","1")
.header("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
.ignoreContentType(true).timeout(200000).data()
.get();
//获取json
text = doc.text();
System.out.println(text);
JSONObject ojson = new JSONObject(text);
    // JSONArray ajson = (JSONArray) ojson.get("data");
     JSONObject oojson=(JSONObject) ojson.get("data");
    
     String shop_name=(String) oojson.get("name");
     String shop_url="https:"+(String) ((JSONObject) ((JSONArray) oojson.get("shop")).get(0)).get("link");
     String shop_address=(String) ((JSONObject) ((JSONArray) oojson.get("shop")).get(0)).get("address");
    
     String tellphone=(String) ((JSONObject) ((JSONArray) oojson.get("shop")).get(0)).get("phone");//电话号码
    
System.out.println("店铺名称="+shop_name);
System.out.println("shop_url="+shop_url);
System.out.println("店铺地址 ="+shop_address);
System.out.println("电话="+tellphone);
   
try {
//(shop_id, site_ent_id, platform_code,shop_name,shop_address,shop_url,tellphone,type)
             ps.setString(1,CommonUtil.getUUID32());
ps.setString(2,CommonUtil.getUUID32());
ps.setString(3,"010");
ps.setString(4,shop_name);
ps.setString(5,shop_address);
ps.setString(6,shop_url);
ps.setString(7,tellphone);
ps.setString(8,good_type);
ps.executeUpdate();


} catch (Exception e1) {
// TODO Auto-generated catch block

}
conOrcale2.close();
        System.out.println("---------------------------------------------------------------------------------------------");
        if(shop_url.length()>0){
        return shop_url;
        }
        return null;
       
}



}

猜你喜欢

转载自st4024589553.iteye.com/blog/2334697