gpt4 book ai didi

Android : Extract article main content. 如何仅提取内容img并删除图标img

转载 作者:行者123 更新时间:2023-11-30 01:25:38 24 4
gpt4 key购买 nike

我正在使用 JSoup 库提取文本元素并将它们显示在 TextView 中,并使用 ImageLoaderImageView 中显示图像。 ImageLoader 是我创建的用于加载和缓存图像的类。我的以下代码成功显示了文本和图像。但是,我只想在主要内容中显示 img 。我可以知道如何删除所有图标 img 吗?以下是我的代码。

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout)findViewById(R.id.linearLayout1);
setContentToView();
}

public void setContentToView(){
List<String> p = new ArrayList<>();
List<String> src = new ArrayList<>();
Document doc = Jsoup.parse(content);
Elements elements = doc.getAllElements();
String body;

for(Element element :elements ){
Tag tag = element.tag();
if(tag.getName().equalsIgnoreCase("p") ){
element.select("img").remove();
body= element.html() + "<br>";
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText(Html.fromHtml(body));
this.linearLayout.addView(textView);
p.add(body);
}


if (tag.getName().equalsIgnoreCase("img")){
String url = element.select("img").attr("src");
String urls = "http://www.tutorialspoint.com"+ url;
int loader = R.mipmap.ic_launcher;
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(urls, loader, imageView);
this.linearLayout.addView(imageView);
src.add(urls);
}
}
}

下面是代码的结果。但我想删除所有不必要的图标 img。

enter image description here enter image description here

最佳答案

I want to remove all unnecessary icon img.

在提取数据之前,去除包含图标img的div(见下面的示例代码)

样本

String url = "http://www.tutorialspoint.com/joomla/joomla_control_panel.htm";

Document doc = Jsoup.connect(url).timeout(0).get();
System.out.println("BEFORE: " + doc.select("img[src$=Icon.jpg]").size());

doc.select("#rightbar").remove();
System.out.println("AFTER: " + doc.select("img[src$=Icon.jpg]").size());

输出

BEFORE: 5
AFTER: 0

最后一点,代码正在尝试解析 img url。 Jsoup 可以做到。所以不是:

String url  = element.select("img").attr("src");
String urls = "http://www.tutorialspoint.com"+ url;

使用这个:

String urls  = element.absUrl("src"); // Jsoup will construct the absolute url for you

关于Android : Extract article main content. 如何仅提取内容img并删除图标img,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com