简体   繁体   English

在Java代码中,如何提取随机html页面的文本?

[英]In Java code, how can I extract text of a random html page?

I solved this way: 我这样解决了:

String url = ("http://www.repubblica.it/economia/finanza/2011/10/27/news/la_fine_dell_incertezza_solleva_le_azioni_bancarie_in_borsa_alle_italiane_mancano_15_miliardi_di_capitale_met_di_unicredit-23967707/");

Document doc = Jsoup.parse(new URL(url), 2000);

Elements body = doc.select("body");

String s=body.text();

System.out.println(s);

I still have another problem. 我还有另一个问题。 I just want the main text without a title. 我只想要没有标题的正文。 Who can help me? 谁能帮我?


I need an algorithm that extracts the text from websites. 我需要一种从网站提取文本的算法。 I want this text is clean from the tags, classes, etc. and I want that this algorithm can be applied to any web page. 我希望此文本可以从标签,类等中清除,并且希望此算法可以应用于任何网页。

For example for this page 例如此页面

I need the main text: 我需要正文:

MILANO - Il tanto atteso responso sui fabbisogni di patrimonio delle maggiori banche europee è arrivato. 米兰-Il tanto atteso responso sui fabbisogni di patrimonio delle maggiori banche europeeèarrivato。 L'Eba (l'Autorità di controllo bancaria europea) ha stabilito la necessità, entro giugno 2012, di ricapitalizzare per ben 106,5 miliardi di euro per i 30 gruppi europei più importanti. L'Eba(l'Autoritàdi controllo bancaria europea)ha stabilito lanecessità,entro giugno 2012,di ricapitalizzare per ben 106,5 miliardi di euro per i 30 gruppi europeipiù重要 Sui 70 gruppi considerati, invece, il deficit patrimoniale è di 160 miliard............... Sui 70 gruppi thinkati,invece,il budget patrimonialeèdi 160 miliard ......

For this page 对于此页面

I need the main text: 我需要正文:

TORINO - Effetto Chrysler sui conti Fiat. 都灵-埃菲特·克莱斯勒的续约菲亚特。 Il Lingotto archivia il terzo trimestre con utili in crescita a 17,6 miliardi (8,4 nello stesso trimestre 2010). Il Lingotto archivia il terzo trimestre con utili in crescita a 17,6 miliardi(8,4 nello stesso trimestre 2010)。 Più che triplicato l'utile della gestione ordinaria che passa da 256 a 851 milioni. 256 851 milioni的三倍重复使用法。 Due terzi arrivano da Detroit che................ Due Terzi Arrivano da Detroit che ................

Thanks 谢谢

Try the boilerplate library. 尝试样板库。

Another option would be to explore Apache Tika , which will index content in a meaningful way. 另一个选择是探索Apache Tika ,它将以有意义的方式索引内容。

Note that defining "main text" is largely impossible. 注意,定义“主要文本”在很大程度上是不可能的。 If you know the site you can try to understand their template and make some assumptions. 如果您知道该站点,则可以尝试了解其模板并做出一些假设。 Doing it across random sites is difficult, which is where something like boilerpipe/tika come into play. 随机站点上执行此操作很困难,这就是样机/提卡的作用。

I've just discovered Jsoup and it looks just perfect for what you want 我刚刚发现了Jsoup ,它看起来非常适合您想要的东西

Seems that something along these lines will extract that text from "div_Id" 似乎这些内容会从“ div_Id”中提取该文本

Document doc = Jsoup.connect("http://www.repubblica.it/economia/finanza/2011/10/27/news/la_fine_dell_incertezza_solleva_le_azioni_bancarie_in_borsa_alle_italiane_mancano_15_miliardi_di_capitale_met_di_unicredit-23967707/").get();
String text = doc.body().id("div_Id").text()

I'm not the expert on this lib, but indeed is way more easier that httpCommonsClient 我不是该库的专家,但确实比httpCommonsClient更容易

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

相关问题 如何仅从根元素中提取文本内容 - java, com.gargoylesoftware.htmlunit.html - How can I extract text content only from root element - java, com.gargoylesoftware.htmlunit.html Java-Jsoup如何在此HTML代码中提取标题和链接? - Java - Jsoup How would I extract the title and link in this HTML code? 如何从安全URL(https://)中提取HTML代码? - How can i extract HTML code from secure URLs (https://)? 如何从 html 代码中提取 web 应用程序内容? - How can I extract web app content from html code? 如何使用PHP或Java从HTML中提取RDFa? - How can I extract RDFa from HTML using PHP or Java? 我如何在JAVA中的代码上获取随机元素? - How i can get Random element on code in JAVA? 能不能通过我的java代码生成HTML页面的xpath - Can I generate the xpath of the HTML page through my java code 如何使用JSoup提取html代码中的特定文本 - How to extract specific text in html code with JSoup 如何使用 Selenium 和 Java 在 https://www.phptravels.net/ 网站中提取 HTML5 约束验证的文本? - How can I extract the text of HTML5 Constraint validation in https://www.phptravels.net/ website using Selenium and Java? 如何在 java 中提取 html 响应的特定文本 - How to extract specific text of a html response in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM