[英]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.