JSoup没有显示正确的文本 [英] JSoup not showing correct text

查看:80
本文介绍了JSoup没有显示正确的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想创建一个Java应用程序,该应用程序将爬网名为chillstep.info的网站的Songname并将其保存到.txt文件中.但是,JSoup将其打印出来:
<div id="titel"> ♫ </div>

So I wanted to create a Java App which crawls the Songname of a website called chillstep.info and saves it into a .txt file. However JSoup prints this out:
<div id="titel"> ♫ </div>

这是代码:

public class Crawltitle {

    public static void getTitle() throws IOException{
        Document doc = Jsoup.connect("http://chillstep.info/").get();
        String title = doc.getElementById("titel").outerHtml();
        System.out.println(title);
    }

    public static void main(String[] args) throws IOException{
        getTitle();
    }
}   

这个问题是由于网站(如果是,为什么以及如何解决该问题)或JSoups造成的吗?

Is this problem because of the website (if yes, why and how to solve that problem) or JSoups?

推荐答案

标题通过

http://chillstep.info/jsonInfo.php

如果忽略通常允许的内容类型,您仍然可以使用Jsoup来获取它:

You still can use Jsoup to get this, if you ignore the usually allowed content type:

Connection con = Jsoup
   .connect("http://chillstep.info/jsonInfo.php")
   .ignoreContentType(true);    
Response res = con.execute();
String rawJSON = res.body();

请注意,我没有使用JSoup解析器.因此,您可能还使用了其他任何库来获取HTTP内容,例如Apache HtmlClient或此类.

Note that I did not use the JSoup parser. So you might as well have used any other library to get HTTP content, like Apache HtmlClient or such.

这时,您可以使用您选择的json库解析answser.或者,因为它是如此简单,所以可以手工"完成:

At this point you can parse the answser with a json library of your choice. Or do it "by hand" since it is so simple:

String title = rawJSON.replaceAll(".*:\"([^\"]*).*","$1");

这篇关于JSoup没有显示正确的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆