1 三元表达式:
divs.size() < 2 ? divs.size() : 2
2 解析出来的格式
字符串:response.getRawText()
DOM对象:Jsoup.parse(response.getRawText())
Json数组对象 JSONArray array = JSONArray.fromObject(response.getRawText())
Json对象 JSONObject json= JSONObject.fromobject(response.getRawText())
3 时间格式
ExtractTime.getDate(“字符串格式的转时间”)
4 字符串操作
替换:URL.replace(“{ID}”,”要用来替换进去的字符串”);
筛选:StringUtil.regexGroup(“总点击:([0-9]+)”, str);
str = str.substring(5);(包括第五个开始)
字符串转Int float double等等: Int.valueOf(str), Float.valueOf(str)….
包含: str.contains(“VIP”)
字符串转list:
一般需要将很多字符串存入list 然后listtoString
listToString(detail.select(“div.book-info>p.tag-box>span>i”))
List list = new ArrayList();
String chapter1 = null;
for (Element element : chapters) {
String ss = element.text();
chapter1 = StringUtil.regexGroup("[.](.*)", element.text());
list.add(chapter1);
}
String chapterStr = listToString(list);
5存入绝对路径(有时候路径不完整)
Document doc = Jsoup.parse(response.getRawText(), **author.getAuthor_url()**);
author.setAuthor_name(doc.select(".author-name").text().trim());
Elements bookList = doc.select(".book-item-tb>li>a>div>h2");
String bookStr = listToString(bookList);
author.setPublish_book(bookStr);
author.setAuthor_img(doc.select(".user-img img").attr(**"abs:src"**));
6对“万字”的处理
String numStr = doc.select("#body>div.book_head>div:nth-child(1)>div.conter_list_box>ul>li>a.conter_li_img_text>div.conter_li_text>div:contains(字数:)>strong").text();
numStr = numStr.replace("字数:", "");
double newNum = 0.0;
if (numStr.contains("万字")) {
newNum = Double.valueOf(numStr.replace("万字", "")) * 10000;
}
book.setBook_num((int) newNum);8
7 移除特定的选择 remove
Elements chapters = doc.select("body>div.Main.List dl");
if (chapters.first().select("dt>span.tit").text().trim().contains("作品相关")) {
//remove 后面有参数
chapters.remove(chapters.first());
}
//无参数
chapters.select("dt").remove();
String chapterStr = listToString(chapters.select("dd>a"));
8 Json啊
Json有两种格式 :对字符串解析 得到json对象或者json数组
json对象:
JSONObject jsonObject =JSONObject.fromObject(response.getRawText());
//一层一层地解析 到最后 没有1234这种索引
Long long = jsonObject.getJSONObject("data").getJSONObject("click_info").getLong("total_count");
json数组:
// 将字符串解析成json数组
JSONArray array = JSONArray.fromObject(datas);
JSONArray object = null;
JSONObject json = null;
//取json数组中的第二个
object = array.getJSONArray(2);
for (int z = 0; z < object.size(); z++) {
BookInfo book = new BookInfo();
AuthorInfo author = new AuthorInfo();
//对json数组里中第二个进行遍历 得到里面每个json字符串
json = object.getJSONObject(z);
book.setBook_name(json.get("title").toString());
book.setAuthor_id(json.get("authorid").toString());
}