爬虫抓取网页数据(研究webmagic,,发现自己发现自己 )
优采云 发布时间: 2021-11-27 17:09爬虫抓取网页数据(研究webmagic,,发现自己发现自己
)
今天(17-03-3 1) 忙了一个下午的webmagic,发现自己还太年轻,做不到这么难的框架(类库)
还是很难接受,先从基础开始吧,因为基础教程比较多,所以找了apache。
HttpClient,根据前辈贴的教程,也写了一个简单的,感觉不错。
以下是单个页面的获取:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
try {
//创建client实例
HttpClient client= HttpClients.createDefault();
//创建httpget实例
HttpGet httpGet=new HttpGet("http://www.btba.com.cn");
//执行 get请求
HttpResponse response=client.execute(httpGet);
//返回获取实体
HttpEntity entity=response.getEntity();
//获取网页内容,指定编码
String web= EntityUtils.toString(entity,"UTF-8");
//输出网页
System.out.println(web);
} catch (IOException e) {
e.printStackTrace();
}
}
}
部分截图显示: