直接上代码:
public String getpricebyinternet(String href,Rfq_Detail record,HttpServletRequest request){
String Path = request.getSession().getServletContext().getRealPath("/WEB-INF/file/"+"proxy.properties");//获取代理资源文件路径
double minprice=10.0;
FileInputStream in = null;
try {
Properties properties = new Properties(); //实例Properties对象
in = new FileInputStream(Path);//读取资源文件
properties.load(in);//加载资源文件
String status=properties.getProperty("status");//获取资源文件内容
if (status.equals("true")) {
String ip=properties.getProperty("ip");//获取资源文件中名为IP的值
int port=Integer.valueOf(properties.getProperty("port"));//获取资源文件中名为port的值
String username=properties.getProperty("username");//获取资源文件中名为username的值
String password=properties.getProperty("password");//获取资源文件中名为password的值
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));//设置代理,如不需代理可注释
Authenticator.setDefault(new BasicAuthenticator(username, password));//设置代理用户名和密码,如不需可注释
URL url = new URL(href);//实例化URL,href为要抓取网站的地址
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection(proxy); //打开连接
urlcon.connect();
//获取连接
InputStream is = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
StringBuffer bs = new StringBuffer();
String l = null;
while((l=buffer.readLine())!=null){
bs.append(l);
}
Document doc = Jsoup.parse(bs.toString()); //jsoup
Element div = doc.getElementById("list1588");
Elements table=div.select("table");
Elements tbody=table.select("tbody");
// 使用选择器选择该table内所有的<tr> <tr/>
Elements trs = tbody.select("tr");
//遍历该表格内的所有的<tr> <tr/>
for (int i = 0; i < trs.size(); ++i) {
// 获取一个tr
Element tr = trs.get(i);
// 获取该行的所有td节点
Elements tds = tr.select("td");
// 选择某一个td节点
for (int j = 0; j < tds.size(); ++j) {
Element td = tds.get(j);
// 获取td节点的所有li
Elements lis = td.select("li");
// 选择一个div
for (int k = 0; k < lis.size(); k++) {
Element li = lis.get(k);
//获取文本信息
String text = li.text();
if (!li.text().equals("See More")) {
double text2=Double.valueOf(text.substring(text.indexOf("$")+1));
if (text2<minprice) {
minprice=text2;
}
}
}
}
}
String minprice2="$".concat(String.valueOf(minprice));
}
} catch (Exception e) {
e.printStackTrace();
}
return minprice2;
}