|
| 1 | +package com.code.repository.study.http; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.apache.http.HttpEntity; |
| 6 | +import org.apache.http.HttpResponse; |
| 7 | +import org.apache.http.client.HttpClient; |
| 8 | +import org.apache.http.client.methods.HttpGet; |
| 9 | +import org.apache.http.impl.client.HttpClients; |
| 10 | +import org.apache.http.util.EntityUtils; |
| 11 | +import org.jsoup.Jsoup; |
| 12 | +import org.jsoup.nodes.Document; |
| 13 | +import org.jsoup.nodes.Element; |
| 14 | +import org.jsoup.select.Elements; |
| 15 | + |
| 16 | +import java.io.BufferedWriter; |
| 17 | +import java.io.File; |
| 18 | +import java.io.FileOutputStream; |
| 19 | +import java.io.OutputStreamWriter; |
| 20 | +import java.net.URI; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.ExecutorService; |
| 24 | +import java.util.concurrent.Executors; |
| 25 | + |
| 26 | +/** |
| 27 | + * @Author zhaoyuan.lizy on 2019/9/5 |
| 28 | + **/ |
| 29 | + |
| 30 | +public class HscodeGatherer implements Runnable{ |
| 31 | + |
| 32 | + private int start=0; |
| 33 | + |
| 34 | + private int end=0; |
| 35 | + |
| 36 | + HscodeGatherer(int start,int end){ |
| 37 | + this.start = start; |
| 38 | + this.end = end; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + public static void main(String[] args){ |
| 43 | + |
| 44 | + ExecutorService executor = Executors.newCachedThreadPool(); |
| 45 | + |
| 46 | + |
| 47 | + String url = "https://www.customs.gov.vn/SitePages/Tariff-Search.aspx?portlet=DetailsImportTax&language=en-US&code="; |
| 48 | + String hscode = "62043300"; |
| 49 | + HscodeGatherer.generateCode(62,63); |
| 50 | + // 爬取数据 |
| 51 | +// Map<String,String> result = HscodeGatherer.fetchInfo(url+hscode); // 爬取数据 |
| 52 | +// result.put("hscode",hscode); // 填充hscode |
| 53 | +// System.out.println(JSON.toJSONString(result)); |
| 54 | +// HscodeGatherer.writeToFile(result);// 写文件 |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + this.generateCode(start,end); |
| 61 | + } |
| 62 | + |
| 63 | + private static String generateCode(int start,int end){ // 开头两位 01-97,一共8位 |
| 64 | + if(start<=0){ |
| 65 | + start=1; |
| 66 | + } |
| 67 | + if(end>99){ |
| 68 | + end=99; |
| 69 | + } |
| 70 | + int start1 = start; |
| 71 | + int start2 = 1; |
| 72 | + int start3 = 1; |
| 73 | + int start4 = 0; |
| 74 | + for(;start1<=end;start1++){ |
| 75 | + for(;start2 <=99;start2++){ |
| 76 | + for(;start3 <=99;start3++){ |
| 77 | + for(;start4 <=99;start4++){ |
| 78 | + String hscode = HscodeGatherer.numToStr(start1)+HscodeGatherer.numToStr(start2)+HscodeGatherer.numToStr(start3)+HscodeGatherer.numToStr(start4); |
| 79 | + System.out.println("=====hscode:"+hscode); |
| 80 | + String url = "https://www.customs.gov.vn/SitePages/Tariff-Search.aspx?portlet=DetailsImportTax&language=en-US&code="; |
| 81 | + Map<String,String> result = HscodeGatherer.fetchInfo(url+hscode); // 爬取数据 |
| 82 | + if(result == null){ |
| 83 | + System.out.println("=====no result:"); |
| 84 | + continue; |
| 85 | + } |
| 86 | + result.put("hscode",hscode); // 填充hscode |
| 87 | + System.out.println(JSON.toJSONString(result)); |
| 88 | + HscodeGatherer.writeToFile(result);// 写文件 |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + // 写文件 |
| 97 | + private static void writeToFile(Map<String,String> result){ |
| 98 | + String fileName = "D:\\hscodeTH.txt"; |
| 99 | + BufferedWriter out = null; |
| 100 | + try { |
| 101 | + OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"UTF-8"); |
| 102 | + out = new BufferedWriter(ow); |
| 103 | + out.newLine(); |
| 104 | + out.write(result.get("hscode")+";"+result.get("Favour")+";"+result.get("ASEAN - China (ACFTA)")); |
| 105 | + out.flush(); |
| 106 | + out.close(); |
| 107 | + } catch (Exception e) { |
| 108 | + }finally{ |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static String numToStr(int num){ |
| 113 | + if(num<9){ |
| 114 | + return "0"+num; |
| 115 | + }else{ |
| 116 | + return String.valueOf(num); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static Map<String,String> fetchInfo(String url){ |
| 121 | + |
| 122 | + String html = HscodeGatherer.doGet(url); |
| 123 | + if(StringUtils.isBlank(html)){ |
| 124 | + return null; |
| 125 | + } |
| 126 | + if(html.contains("Not found")){ // Not found |
| 127 | + System.out.println("===Not found:"); |
| 128 | + return null; |
| 129 | + } |
| 130 | + Document doc = Jsoup.parse(html);// 结构化解析 |
| 131 | + Elements tables = doc.select("table"); |
| 132 | + for(Element eTable : tables){ |
| 133 | + if (eTable.text().contains("Tax rate")) {// 锁定税率列表,提取各种税率 |
| 134 | + Map<String,String> result = new HashMap<>(); |
| 135 | + Elements trs = eTable.select("tr"); |
| 136 | + for(Element tr:trs){ |
| 137 | + Elements tds = tr.select("td"); |
| 138 | + if(tds.size()<2){ |
| 139 | + continue; |
| 140 | + } |
| 141 | + if(StringUtils.isNotBlank(tds.get(1).text())){ // 税率列不为空,记录 |
| 142 | + result.put(tds.get(0).text(),tds.get(1).text()); |
| 143 | + } |
| 144 | + } |
| 145 | + return result; |
| 146 | + }else{ |
| 147 | + continue; |
| 148 | + } |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + private static String doGet(String url){ |
| 154 | + HttpClient client = HttpClients.createDefault(); |
| 155 | + HttpGet request = new HttpGet(); |
| 156 | + try { |
| 157 | + // 初始化post请求头 |
| 158 | + request.addHeader("Content-Type", "text/html; charset=utf-8"); |
| 159 | + request.setURI(new URI(url)); |
| 160 | + HttpResponse response = client.execute(request); |
| 161 | + // 解析结果 |
| 162 | + int code = response.getStatusLine().getStatusCode(); |
| 163 | + if (code == 200) { // 请求成功 |
| 164 | + HttpEntity entity = response.getEntity(); |
| 165 | + return EntityUtils.toString(entity, "utf-8"); |
| 166 | + }else { |
| 167 | + System.out.println("===http status:"+code); |
| 168 | + } |
| 169 | + }catch(Exception e){ |
| 170 | + } |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | +} |
0 commit comments