您的位置:首页 > 其它

简单爬虫实战

2015-07-31 16:26 351 查看
1. 某p2p网站每天发新标,对于一个标最后投标导致标满的用户,系统会奖励38元红包,所以写啦个爬虫每隔1分钟去爬取合适的标,然后短信提醒

2. 两个要爬取的页面

============投资列表=====================



======标的详情页面如下==============



=============================

3.核心代码逻辑

public Document getDocument (String url){
try {
return Jsoup.connect(url).get();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


//1.首页抓取未完成的标
CrawUtil ft = new CrawUtil();
Document fDoc = ft.getDocument("http://www.qtyd.com/");
Elements elements=fDoc.select("dd.invset-item");
if(elements.size()<=7){
//标的列表低于7个标
return ;
}
try {
//2.循环判断每个标的情况
for(int i=0;i<=7;i++){
//获取进度
Element elements1=elements.get(i);
Elements speed=elements1.select(".item-speed");
Elements speedLine=speed.select(".invest-speed-line");
String iSpeedLine= speedLine.toString();
String speedStr = iSpeedLine.substring(iSpeedLine.lastIndexOf(":")+1,iSpeedLine.lastIndexOf("%")).trim();
//过滤掉满标的
if(Double.parseDouble(speedStr)==100.00){
continue;
}
//获取收益
String itemApr=elements1.select(".item-apr").text();
//                if("12.00%".equals(itemApr.toString())){
//                    continue;
//                }
//3.获取天数
String itemTime=elements1.select(".item-time").text();
itemTime=itemTime.replace("天","").trim();
//                if(Integer.parseInt(itemTime)>50){
//                    continue;
//                }
//获取标id
String itemName=elements1.select(".item-name").toString();
itemName=itemName.substring(itemName.lastIndexOf("invest/")+7,itemName.lastIndexOf(".html"));

CrawUtil t = new CrawUtil();
String url=String.format("http://www.qtyd.com/invest/%s.html",Integer.parseInt(itemName));
Document doc = t.getDocument(url);
// 获取目标HTML代码
Elements ele1 = doc.select(".ft30");
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("监控到有标可以投资:");
// 可投金额
String money = ele1.get(0).text().replace(",","");
//                if(Double.parseDouble(money)>50000){
//                    continue;
//                }
stringBuffer.append("可投金额:" + money);

// 年利率
String rank = ele1.get(1).text();
stringBuffer.append("年利率:" + rank);

// days
String days = ele1.get(2).text();
stringBuffer.append("收益期限" + days + "天"+"\n");
stringBuffer.append("请访问:"+url+"\n");
System.out.print(stringBuffer.toString());
//发短信通知
//SmsUtil.sendSms(stringBuffer.toString(),"18758160514");
}
} catch (Exception e) {
e.printStackTrace();
}


4.爬取信息



5.后续扩展



(1)该网站各种请求url如上,后续有时间结合如上地址可以搞个自动投标插件,实现自动抢标、投标
6.遇到问题
之前get请求可以投标,但现在他们已经做了限制,不能get或post请求,请求报错信息如下:



如上问题查了很多资料,说清空本地cookie但试啦还是不行,还有说这个是php后台设置,以后有时间再尝试下是否存在csrf攻击……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: