简体   繁体   English

Jsoup无法解析我的网站

[英]Jsoup won't parse my website

I am trying to get the title of the site, the code checks if the string contains the title is not 'null'. 我正在尝试获取网站的标题,代码将检查包含标题的字符串是否为'null'。 If it isn't null, it will execute the rest of the code. 如果不为null,它将执行其余代码。

try {
    Document htmlDocument = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").userAgent("Mozilla").get();
    htmlContentInString = htmlDocument.title();
    if(htmlContentInString != null) {
        isNull = false;
            }
} catch (IOException e) {
    e.printStackTrace();
}

The problem is 问题是

htmlContentInString = htmlDocument.title();
if(htmlContentInString != null)

The application skips that 'if statement' as it didn't receive the title. 应用程序跳过该“ if语句”,因为它没有收到标题。 But the code is working perfectly on other websites. 但是该代码在其他网站上运行良好。

I don't know is this something that you want but I think this might help you. 我不知道这是您想要的东西,但我认为这可能对您有所帮助。 I have some jsoup project with custom adapter and listview so I little adjust it according to your needs and I got this. 我有一些带有自定义适配器和listview的jsoup项目,所以我很少根据您的需要对其进行调整,我明白了。 Basically I wanted to show you how can you parse your html with jsoup. 基本上,我想向您展示如何使用jsoup解析html。 I'm using AsyncTask to get data from url and you should use to: 我正在使用AsyncTask从url获取数据,您应该使用以下方法:

try {
            document = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").get();
            Elements links = document.getElementsByClass("single-product");
            for (Element element : links) {
                Data data = new Data(); //class where I set and get the data, of course you don't have to follow this patern

                Elements getLink = element.select("a.product_link[href]");
                String link = getLink.attr("abs:href");
                Elements getImage = element.select("img.product_poster[src]");
                String image = getImage.attr("abs:src");
                String title = element.select("p.product_name").text();
                String price = element.select("p.product_price").text();
                Log.d(image, title);
                data.setUrl(link);
                data.setTopic(title);
                data.setBitmap(getBitmap(image));
                datas.add(data);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
     }

Method for downloading the images if you need it: 必要时下载图像的方法:

public Bitmap getBitmap(String src)
{
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap myBitmap = BitmapFactory.decodeStream(inputStream, null, options);
        return myBitmap;
    }
    catch (IOException e)
    {
        e.printStackTrace();
        return  null;
    }
}

And this is the result: 结果如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM