jsoup爬虫,项目实战,欢迎收看
import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements public class ZhongYuan {
public static final DBCollection test = MongoUtils.getCollectionByName("name", "table",
"port")
public static final DBCollection html = MongoUtils.getCollectionByName("name", "table",
"port") public static void main(String[] args){
// 循环遍历页面进行数据爬去
for(int i = 500 ; i<598 ;i++) {
String url = "http://sh.centanet.com/xiaoqu/g"+i+"/";
String result = RequestUtil.doGet(url, "GBK");
Document doc = Jsoup.parse(result);
//页面加载完成后对document进行处理,获取自己有用的数据
parseList(doc);
System.out.println("page=====>"+i);
}
}
private static void parseList(Document doc){ Elements elements = doc.select("div.house-listBox>div");
int j = 0;
for(Element element : elements){ String name = element.select(".house-title a").first().text();
html.save(new BasicDBObject("name",name).append("html",element.toString()))
String regionstr = element.select("div>div>p").first().text().replace(' ','-');
String region = regionstr.split("-")[0];
String address = null;
if(regionstr.split("-").length>1) {
address = regionstr.split("-")[1] + regionstr.split("-")[2];
} else {
address = regionstr.split("-")[1];
} String price = element.select("div>div").last().select("p").first().text();
test.insert(new BasicDBObject("city","上海").append("region",region).append("name",name)
.append("avg_price",price));
System.out.println(name);
j++;
}
System.out.println(j);
}
private static void parseList1(Document doc) {
Elements elements = doc.select("div.section>ul>li");
String name = null;
String region = null;
String price = null;
for (Element element : elements) {
if (element.toString().contains("room-img")) {
name = element.select("h5.room-name a").first().text();
Elements datas = element.select("p");
int i = 0;
for (Element data : datas) {
i++;
if (i == 2) {
price = data.text();
}
if (i == 4) {
region = data.text();
}
}
System.out.println(name + price + region);
test.insert(new BasicDBObject("city","上海").append("region",region).append("name",name)
.append("avg_price",price));
}
}
}
}
相关doget请求自己封装了一个util,可以看看,上面的这一句String result = RequestUtil.doGet(url, "GBK");用的就是自己封装的util包,这里也可以使用jsoup自己封装的。
/**
* 发送get请求
* @param url
* @return
*/ public static String doGet(String url) {
return doGet(url, null, "UTF-8", false);
} public static String doGet(String url, boolean encodeUrl) {
return doGet(url, null, "UTF-8", encodeUrl);
} public static String doGet(String url, String charset) {
return doGet(url, null, charset, true);
} public static String doGet(String url, Map<String, String> headers) {
return doGet(url, headers, "UTF-8", true);
} public static String doGet(final String url, Map<String, String> headers, String charset, boolean encodeUrl) {
CloseableHttpClient client = HttpClients
.custom()
.setUserAgent(USERAGENT_CHROME)
.build(); CloseableHttpResponse response = null;
String result = null;
String requestUrl = url;
try {
if(encodeUrl) {
requestUrl = encodingUrl(url, charset);
}
HttpGet httpGet = new HttpGet(requestUrl);
// RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
// httpGet.setConfig(requestConfig);
if(headers != null) {
for(Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue());
}
}
response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200) {
result = EntityUtils.toString(response.getEntity(), charset);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(response != null) {
try {
response.close();
} catch (IOException e) {
}
}
if(client != null) {
try {
client.close();
} catch (IOException e) {
}
}
}
return result;
}
jsoup爬虫,项目实战,欢迎收看的更多相关文章
- Go语言之高级篇Beego框架之爬虫项目实战
一.爬虫项目 1.爬虫基础 a.网页上面会有相同的数据 b.去重处理 布隆过滤器哈希存储 c.标签匹配: 正则表达式beautiful soup或lxml这种标签提取库 d.动态内容 phantomj ...
- Java爬虫项目实战(一)
目的: 通过网络爬虫爬取中国最小粒度的区域维度信息,包括省(Province) .市(City).县(County).镇(town).村委会(village) 主网站链接: http://www.st ...
- 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo
简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...
- Python爬虫开发与项目实战
Python爬虫开发与项目实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1MFexF6S4No_FtC5U2GCKqQ 提取码:gtz1 复制这段内容后打开百度 ...
- 学习推荐《精通Python网络爬虫:核心技术、框架与项目实战》中文PDF+源代码
随着大数据时代的到来,我们经常需要在海量数据的互联网环境中搜集一些特定的数据并对其进行分析,我们可以使用网络爬虫对这些特定的数据进行爬取,并对一些无关的数据进行过滤,将目标数据筛选出来.对特定的数据进 ...
- Python爬虫开发与项目实战pdf电子书|网盘链接带提取码直接提取|
Python爬虫开发与项目实战从基本的爬虫原理开始讲解,通过介绍Pthyon编程语言与HTML基础知识引领读者入门,之后根据当前风起云涌的云计算.大数据热潮,重点讲述了云计算的相关内容及其在爬虫中的应 ...
- Scrapy爬虫框架第八讲【项目实战篇:知乎用户信息抓取】--本文参考静觅博主所写
思路分析: (1)选定起始人(即选择关注数和粉丝数较多的人--大V) (2)获取该大V的个人信息 (3)获取关注列表用户信息 (4)获取粉丝列表用户信息 (5)重复(2)(3)(4)步实现全知乎用户爬 ...
- 给缺少Python项目实战经验的人
我们在学习过程中最容易犯的一个错误就是:看的多动手的少,特别是对于一些项目的开发学习就更少了! 没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用 ...
- Python项目实战:福布斯系列之数据采集
1 数据采集概述 开始一个数据分析项目,首先需要做的就是get到原始数据,获得原始数据的方法有多种途径.比如: 获取数据集(dataset)文件 使用爬虫采集数据 直接获得excel.csv及其他数据 ...
- Python NLP完整项目实战教程(1)
一.前言 打算写一个系列的关于自然语言处理技术的文章<Python NLP完整项目实战>,本文算是系列文章的起始篇,为了能够有效集合实际应用场景,避免为了学习而学习,考虑结合一个具体的项目 ...
随机推荐
- 【Lintcode】011.Search Range in Binary Search Tree
题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...
- ORA-00119: invalid specification for system parameter REMOTE_LISTENER
环境说明: RAC 启动数据库报 ORA-00119: invalid specification for system parameter REMOTE_LISTENER . 检查 list ...
- Elasticsearch搜索引擎版本配置
简要描述: 搜索引擎版本配置 产品 版本号 ES版本要求 说明 PHP =5.5.38 Java =1.8.0_73 用于支持ES Elasticsearch =2.3.5 搜索引擎 ...
- Spring 3.1新特性之三:Spring支持Servlet 3.0(待补充)
高效并发是JDK 1.6的一个重要主题,HotSpot虚拟机开发团队在这个版本上花费了大量的精力去实现各种锁优化技术,如适应性自旋(Adaptive Spinning).锁削除(Lock Elimin ...
- Windows WMIC命令使用详解1
https://blog.csdn.net/enweitech/article/details/51982114 在CMD和Powershell中 使用WMIC 先决条件: a. 启动Windows ...
- mysql create table
- eos管理页面
调用此方法删除需要在po_module_processdef添加数据如下 默认情况下申请页面是有权限的 但是在此表加过之后 管理页面要打开拟稿页面还必须在 系统管理页面(xtgl.jsp ) 分 ...
- 微软 Remote App
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wangchunhai.blog.51cto.com/225186/201073 ...
- VisualStudio2017中新建的ASP.NET Core项目中的各个文件的含义
Program.cs is the entry point for the web application; everything starts from here. As we mentione ...
- 通俗易懂,什么是.NET Core以及.NET Core能做什么
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/10880884.html 我们都知道.NET Core是一个可以用来构建现代.可伸缩和高性能的跨平台软件应 ...