簡體   English   中英

Jsoup無法解析我的網站

[英]Jsoup won't parse my website

我正在嘗試獲取網站的標題,代碼將檢查包含標題的字符串是否為'null'。 如果不為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();
}

問題是

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

應用程序跳過該“ if語句”,因為它沒有收到標題。 但是該代碼在其他網站上運行良好。

我不知道這是您想要的東西,但我認為這可能對您有所幫助。 我有一些帶有自定義適配器和listview的jsoup項目,所以我很少根據您的需要對其進行調整,我明白了。 基本上,我想向您展示如何使用jsoup解析html。 我正在使用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();
        }
     }

必要時下載圖像的方法:

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;
    }
}

結果如下:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM