Java Code Examples for org.jsoup.nodes.Element#getElementsByClass()

The following examples show how to use org.jsoup.nodes.Element#getElementsByClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageDetailSoup.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(Document root, Element head, Element body, Map<String, Object> values) {
    Elements elements = body.getElementsByClass("prev-next-page");
    String text = elements.get(0).text();
    text = text.substring(text.indexOf("/") + 1);
    int maxPage = Integer.valueOf(text.replace("页", ""));
    elements = body.getElementsByTag("figure");
    elements = elements.get(0).getElementsByTag("img");
    String imageUrl = elements.get(0).attr("src");
    String originImageUrl = imageUrl;
    PageDetailModel model = new PageDetailModel((String) getArguments()[0]);
    String suffix = imageUrl.substring(imageUrl.lastIndexOf("."));
    imageUrl = imageUrl.substring(0,imageUrl.lastIndexOf("."));
    imageUrl = imageUrl.substring(0,imageUrl.length()-2);
    imageUrl += "%02d";
    imageUrl += suffix;
    model.setImgUrl(imageUrl);
    model.setMaxLen(maxPage);
    model.setType(0);
    values.put(getClass().getSimpleName(), model);
    values.put("origin_img",originImageUrl);
}
 
Example 2
Source File: XiamiParser.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 获取指定页HTML 文档指定的body
 *
 * @throws IOException
 */
private Set<SongInfo> getSongInfoSet(String blogUrl, int page) throws IOException {
	Set<SongInfo> songList = new HashSet<SongInfo>();
	Document doc = Jsoup.connect(blogUrl + "/" + page).get();
	Elements postTitles = doc.body().getElementsByClass("track_list");
	for (Element postTitle : postTitles) {
		Elements songs = postTitle.getElementsByTag("tr");
		for (Element song : songs) {
			Elements name = song.getElementsByClass("song_name");
			for (Element link : name) {
				SongInfo songinfo = new SongInfo();
				songinfo.setName(link.child(0).text());
				Elements artistName = link.getElementsByClass("artist_name");
				songinfo.setArtist(artistName.get(0).text());
				songList.add(songinfo);
			}
		}
	}
	return songList;
}
 
Example 3
Source File: JokeBean.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
public JokeBean(Element element) {
    //内容
    //得到内容,返回的是元素集合,然后再取第一个数据
    Element tmpContent = element.getElementsByClass("content").first();
    //取出文本
    this.content = tmpContent.text();

    //图片
    //图片地址,有两种可能,有或没有
    Elements tmpThumb = element.getElementsByClass("thumb");
    //如果imgs为null,或者内容长度为0说明没有图片,否则有图片,取第一个即可
    if(tmpThumb !=null && tmpThumb.size()>0){
        //有图片,解析出图片地址,取出第一个元素
        Element tmpImg = tmpThumb.first();
        //得到img标签的选择器,src的属性值即为图片地址
        this.img = tmpImg.select("img").attr("src");

    }
    //链接地址
    //得到class='contentHerf',取出第一个元素,得到a的选择器,取出href属性
    Element tmpHerf = element.getElementsByClass("contentHerf").first();
    this.contentHerf = tmpHerf.select("a").attr("href");

}
 
Example 4
Source File: SetDofus.java    From KaellyBot with GNU General Public License v3.0 6 votes vote down vote up
private static List<String> extractStatsFromTitleToList(Language lg, Element elem)
{
    List<String> values = new ArrayList<>();
    Elements lines = elem.getElementsByClass("ak-title");
    StringBuilder tmp = new StringBuilder();
    for (Element line : lines) {
        String value = EmojiManager.getEmojiForStat(lg, line.text()) + line.text() + "\n";
        if (value.length() + tmp.length() > Embed.Field.MAX_VALUE_LENGTH){
            values.add(tmp.toString());
            tmp.setLength(0);
        }
        else
            tmp.append(value);
    }
    if (tmp.length() > 0)
        values.add(tmp.toString());

    return values;
}
 
Example 5
Source File: JsoupUtils.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Element getElementByClass(Element element, String className) {
    Elements elements = element.getElementsByClass(className);
    if (elements != null && elements.size() > 0) {
        return elements.get(0);
    } else {
        return null;
    }
}
 
Example 6
Source File: InspectorV3.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
private void doSearch(Element document) {
    Elements gal=document.getElementsByClass("gallery");
    galleries=new ArrayList<>(gal.size());
    for(Element e:gal)galleries.add(new SimpleGallery(context.get(),e));
    gal=document.getElementsByClass("last");
    pageCount=gal.size()==0?Math.max(1,page):findTotal(gal.last());
}
 
Example 7
Source File: JsoupHtml.java    From java-study with Apache License 2.0 5 votes vote down vote up
public void getZhiLianWork(){
    try {
        for (int i=0;i<10;i++) {
                System.out.println("*********开始遍历第"+(i+1)+"页的求职信息*********");
                Document doc = Jsoup.connect(url+city+"&kw="+keywords+"&p="+(i+1)+"&isadv=0").get();                    
                Element content = doc.getElementById("newlist_list_content_table"); 
                if(content == null){
                	continue;
                }
                Elements zwmcEls = content.getElementsByClass("zwmc");
                Elements gsmcEls = content.getElementsByClass("gsmc");            
                Elements zwyxEls = content.getElementsByClass("zwyx");            
                Elements gzddEls = content.getElementsByClass("gzdd");            
                Elements gxsjEls = content.getElementsByClass("gxsj");
                for(int j = 0;j<zwmcEls .size();j++){
                    
                    System.out.println(
                            zwmcEls.get(j).tagName("a").text()+"*****"+gsmcEls.get(j).tagName("a").text()+
                            "*****"+zwyxEls.get(j).tagName("a").text()+"*****"+gzddEls.get(j).tagName("a").text()+
                            "*****"+gxsjEls.get(j).tagName("a").text());
                    System.out.println();
            }
                System.out.println("*********结束遍历第"+(i+1)+"页的求职信息*********");
        
        }
        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example 8
Source File: JsoupUtils.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Element getElementByClass(Element element, String className) {
    Elements elements = element.getElementsByClass(className);
    if (elements != null && elements.size() > 0) {
        return elements.get(0);
    } else {
        return null;
    }
}
 
Example 9
Source File: SetDofus.java    From KaellyBot with GNU General Public License v3.0 5 votes vote down vote up
private static String extractStatsFromTitle(Language lg, Element elem) {
    if (elem != null){
        Elements lines = elem.getElementsByClass("ak-title");
        StringBuilder tmp = new StringBuilder();
        for (Element line : lines)
            tmp.append(EmojiManager.getEmojiForStat(lg, line.text())).append(line.text()).append("\n");
        return tmp.toString();
    }
    return null;
}
 
Example 10
Source File: Monster.java    From KaellyBot with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param butins Liste des butins
 * @param element Element contenant les butins
 * @return true si c'est en erreur, false le cas échéant
 */
private static boolean extractButins(List<String> butins, Element element){
    boolean error = false;
    StringBuilder field = new StringBuilder();
    Elements lines = element.getElementsByClass("ak-column");
    for (Element line : lines) {
        StringBuilder tmp = new StringBuilder();
        tmp.append(line.getElementsByClass("ak-front").text());

        Elements titles = line.getElementsByClass("ak-title");
        if (!titles.isEmpty()) {
            if (!titles.first().children().isEmpty())
                tmp.append("[").append(titles.first().text()).append("](")
                        .append(titles.first().children().first().attr("abs:href")).append(") ");
            else
                tmp.append(titles.first().text()).append(" ");

            tmp.append(line.getElementsByClass("ak-drop-percent").first().text()).append("\n");

            if (field.length() + tmp.length() > Embed.Field.MAX_VALUE_LENGTH) {
                butins.add(field.toString());
                field.setLength(0);
            }
            field.append(tmp.toString());
        }
        else error = true;
    }

    if (field.length() > 0)
        butins.add(field.toString());

    return error;
}
 
Example 11
Source File: Resource.java    From KaellyBot with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param element Element contenant les drops
 */
private static List<String> extractDrops(Element element){
    List<String> result = new ArrayList<>();
    StringBuilder field = new StringBuilder();
    Elements lines = element.getElementsByClass("ak-column");
    for (Element line : lines) {
        StringBuilder tmp = new StringBuilder();

        Elements titles = line.getElementsByClass("ak-title");
        if (!titles.isEmpty()) {
            if (!titles.first().children().isEmpty())
                tmp.append("[").append(titles.first().text()).append("](")
                        .append(titles.first().children().first().attr("abs:href")).append(") ");
            else
                tmp.append(titles.first().text()).append(" ");

            tmp.append(line.getElementsByClass("ak-aside").first().text()).append("\n");

            if (field.length() + tmp.length() > Embed.Field.MAX_VALUE_LENGTH) {
                result.add(field.toString());
                field.setLength(0);
            }
            field.append(tmp.toString());
        }
    }

    if (field.length() > 0)
        result.add(field.toString());

    return result;
}
 
Example 12
Source File: ParseHelper.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
private void parseComments(Element main) {
    String comment, link, userName, date, ratingNum, ratingText;
    List<CommentsModel> cache = new ArrayList<>();
    ArrayList<String> dr = new ArrayList<>();
    for (Element element1 : main.select("#comments .reviews li")) {
        if (!element1.select(".text-box").text().isEmpty()) {
            /**
             * Тут короче если текст бокс не нуль, то и все остальное не нуль.
             */
            Element element =  element1.select(".text-box .w-toggle").first();
            if(element==null)
                element = element1.select(".text-box").first();
            comment = element.text();
            element = element1.select("div.name a").first();
            link = element.attr("href");
            userName = element.attr("title");
            date = element1.select("div.date").first().text();
            ratingNum = element1.select("span.num").first().text();
            ratingText = element1.select("span.text").first().text();
            // for detail dialog
            Elements elements1 = element1.getElementsByClass("reviews-list");
            if (elements1 != null)
                for (Element element2 : elements1)
                    dr.add(element2.select("div.line").text());
            cache.add(new CommentsModel(date, ratingNum, ratingText, comment, link, userName, dr));
        }
    }
    parsed.setCommentsModels(new Gson().toJson(cache));
}
 
Example 13
Source File: ThreadListRequest.java    From something.apk with MIT License 5 votes vote down vote up
private static String getFirstTextByClass(Element parent, String htmlClass, String fallback){
    Elements targets = parent.getElementsByClass(htmlClass);
    if(targets.size() > 0){
        return targets.first().text().trim();
    }
    return fallback;
}
 
Example 14
Source File: GalleryDetailParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
/**
 * Parse large previews with regular expressions
 */
private static LargePreviewSet parseLargePreviewSet(Document d, String body) throws ParseException {
    try {
        LargePreviewSet largePreviewSet = new LargePreviewSet();
        Element gdt = d.getElementById("gdt");
        Elements gdtls = gdt.getElementsByClass("gdtl");
        int n = gdtls.size();
        if (n <= 0) {
            throw new ParseException("Can't parse large preview", body);
        }
        for (int i = 0; i < n; i++) {
            Element element = gdtls.get(i).child(0);
            String pageUrl = element.attr("href");
            element = element.child(0);
            String imageUrl = element.attr("src");
            if (Settings.getFixThumbUrl()) {
                imageUrl = EhUrl.getFixedPreviewThumbUrl(imageUrl);
            }
            int index = Integer.parseInt(element.attr("alt")) - 1;
            largePreviewSet.addItem(index, imageUrl, pageUrl);
        }
        return largePreviewSet;
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        throw new ParseException("Can't parse large preview", body);
    }
}
 
Example 15
Source File: FavoritesParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static Result parse(String body) throws Exception {
    if (body.contains("This page requires you to log on.</p>")) {
        throw new EhException(GetText.getString(R.string.need_sign_in));
    }
    String[] catArray = new String[10];
    int[] countArray = new int[10];

    try {
        Document d = Jsoup.parse(body);
        Element ido = JsoupUtils.getElementByClass(d, "ido");
        //noinspection ConstantConditions
        Elements fps = ido.getElementsByClass("fp");
        // Last one is "fp fps"
        AssertUtils.assertEquals(11, fps.size());

        for (int i = 0; i < 10; i++) {
            Element fp = fps.get(i);
            countArray[i] = ParserUtils.parseInt(fp.child(0).text(), 0);
            catArray[i] = ParserUtils.trim(fp.child(2).text());
        }
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        throw new ParseException("Parse favorites error", body);
    }

    GalleryListParser.Result result = GalleryListParser.parse(body);

    Result re = new Result();
    re.catArray = catArray;
    re.countArray = countArray;
    re.pages = result.pages;
    re.nextPage = result.nextPage;
    re.galleryInfoList = result.galleryInfoList;

    return re;
}
 
Example 16
Source File: HtmlParse.java    From ChipHellClient with Apache License 2.0 4 votes vote down vote up
/**
 * 解析板块列表
 *
 * @param content
 * @return
 */
public static List<PlateGroup> parsePlateGroupList(String content) {
    List<PlateGroup> groups = new ArrayList<PlateGroup>();
    Document document = Jsoup.parse(content);
    document.setBaseUri(Constants.BASE_URL);
    Elements elementsGroup = document.getElementsByClass("bm");
    for (Element bm : elementsGroup) {
        PlateGroup plateGroup = new PlateGroup();

        Element bm_h = bm.getElementsByClass("bm_h").first();
        String title = bm_h.text();
        plateGroup.setTitle(title);
        List<Plate> plates = new ArrayList<Plate>();
        Elements plateElements = bm.getElementsByClass("bm_c");

        for (Element bm_c : plateElements) {
            Plate plate = new Plate();
            //链接,第一个是版块链接,如果有第二个则是删除收藏连接
            Elements as = bm_c.getElementsByTag("a");
            Element a1 = as.first();
            String plateTitle = a1.text();
            String url = a1.absUrl("href");
            Elements count = bm_c.getElementsByClass("xg1");
            String xg1 = null;
            if (count.size() != 0) {
                xg1 = count.first().text();
            } else {
                xg1 = "(0)";
            }

            //判断是否收藏
            String favoriteId = null;
            if (as.size() > 1) {
                String urlDelete = as.get(1).absUrl("href");
                favoriteId = new UrlParamsMap(urlDelete).get("favid");
            }

            plate.setTitle(plateTitle);
            plate.setUrl(url);
            plate.setXg1(xg1);
            plate.setFavoriteId(favoriteId);
            plates.add(plate);

        }

        plateGroup.setPlates(plates);
        groups.add(plateGroup);
    }

    return groups;
}
 
Example 17
Source File: FormatUtil.java    From wlmedia with Apache License 2.0 4 votes vote down vote up
public static String formatHomePage(String html, String uri)
{

    StringBuffer buffer = new StringBuffer();
    buffer.append("[");
    Document document = Jsoup.parse(html);
    Elements elements = document.getElementsByClass("bx-sya");
    int size = elements.size();
    for(int i = 0; i < size; i++)
    {
        Element element = elements.get(i);
        Elements hd = element.getElementsByClass("hd");
        Element hd1 = hd.get(0);
        buffer.append("{\"type\":\"");
        buffer.append(hd1.text());
        buffer.append("\",\"values\":[");
        Element bd = element.getElementsByClass("bd").get(0);
        Elements a = bd.select("a");
        int s = a.size();
        for(int j = 0; j < s; j++)
        {
            Element aa = a.get(j);
            buffer.append("{\"name\":\"");
            buffer.append(aa.select("i").text());
            buffer.append("\",\"url\":\"");
            buffer.append(uri);
            buffer.append(aa.attr("href"));
            buffer.append("\"");
            if(j == s - 1)
            {
                buffer.append("}");
            }
            else
            {
                buffer.append("},");
            }
        }
        if(i == size - 1)
        {
            buffer.append("]}");
        }
        else
        {
            buffer.append("]},");
        }
    }
    buffer.append("]");
    return buffer.toString();
}
 
Example 18
Source File: FormatUtil.java    From wlmedia with Apache License 2.0 4 votes vote down vote up
public static String formatHomePage(String html, String uri)
{

    StringBuffer buffer = new StringBuffer();
    buffer.append("[");
    Document document = Jsoup.parse(html);
    Elements elements = document.getElementsByClass("bx-sya");
    int size = elements.size();
    for(int i = 0; i < size; i++)
    {
        Element element = elements.get(i);
        Elements hd = element.getElementsByClass("hd");
        Element hd1 = hd.get(0);
        buffer.append("{\"type\":\"");
        buffer.append(hd1.text());
        buffer.append("\",\"values\":[");
        Element bd = element.getElementsByClass("bd").get(0);
        Elements a = bd.select("a");
        int s = a.size();
        for(int j = 0; j < s; j++)
        {
            Element aa = a.get(j);
            buffer.append("{\"name\":\"");
            buffer.append(aa.select("i").text());
            buffer.append("\",\"url\":\"");
            buffer.append(uri);
            buffer.append(aa.attr("href"));
            buffer.append("\"");
            if(j == s - 1)
            {
                buffer.append("}");
            }
            else
            {
                buffer.append("},");
            }
        }
        if(i == size - 1)
        {
            buffer.append("]}");
        }
        else
        {
            buffer.append("]},");
        }
    }
    buffer.append("]");
    return buffer.toString();
}