我还是统一发一下源码吧,我的源码里还有一个方法是把取到的数据写如excel,大家根据自己的需求改一下吧
[Java] 纯文本查看 复制代码 public class JustTest {
public static void main(String[] args) throws WriteException, IOException {
String html = getHtmlByUrl("http://hrb.meituan.com/category/meishi/pingfangqu?mtt=1.index%2Fdefault%2Fpoi.0.0.ihlo3zbw");
String[][] arr = new String[1024][2];
if (html!=null&&!"".equals(html)) {
Document doc = Jsoup.parse(html);
Elements linksElements = doc.select("div.basic>a");
int i=0;
for (Element ele:linksElements) {
String href = ele.attr("href");
String title = ele.text();
System.out.println(href+","+title);
arr[i][0] = href;
arr[i][1] = title;
}
}
createExcel(arr);
}
public static String getHtmlByUrl(String url){
String html = null;
HttpClient httpClient = new DefaultHttpClient();//创建httpClient对象
HttpGet httpget = new HttpGet(url);//以get方式请求该URL
try {
HttpResponse responce = httpClient.execute(httpget);//得到responce对象
int resStatu = responce.getStatusLine().getStatusCode();//返回码
if (resStatu==HttpStatus.SC_OK) {//200正常 其他就不对
//获得相应实体
HttpEntity entity = responce.getEntity();
if (entity!=null) {
//html = EntityUtils.toString(entity);//获得html源代码
InputStream in = entity.getContent();
entity.getContentType();
Scanner sc = new Scanner(in);
StringBuffer str = new StringBuffer("utf-8");
while(sc.hasNextLine()){
str.append(sc.nextLine());
}
html = str.toString();
//sc.close();
}
}
} catch (Exception e) {
System.out.println("访问【"+url+"】出现异常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
}
public static void createExcel(String[][] allArray) throws WriteException,IOException{
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(new File("D://excel.xls"));
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet",0);
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Label xuhao = new Label(0,0,"商户名称");
sheet.addCell(title);
Label shopname = new Label(1,0,"商户链接");
sheet.addCell(href);
for(int i=0;i<allArray.length;i++){
for(int j=0;j<2;j++){
Label oneInfo = new Label(j+1,i+1,allArray[i][j]);
sheet.addCell(oneInfo);
}
}
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
}
} |