jsoup

How to extract tags and text between tags to a list with JSoup

岁酱吖の 提交于 2020-01-25 08:41:06
问题 I have following html: <div class="CustomClass"> Hi!<br/> <br/> Bla Bla bla<br/> <br/> <a href...></a> bla bla bla <iframe...></iframe> Thank you! </div> I need a list with the children of the div, something like the following: 0->Hi! 2-><br/> 3->Bla Bla bla 4-><br/> 5-><a href...></a> 6->bla bla bla 7-><iframe...></iframe> 8->Thank you! I tried by getting the children of the div element, and then iterating the children and converting them to html, but this returns only the tag elements and

How to wrap a method around an async section of code

北战南征 提交于 2020-01-24 20:52:48
问题 How do I wrap a method around this Async section of codes so I can get the variable "doc" returned as a returned value so I can reuse this method? I can't declare a static method inside this class, and when I tried to use a void method, the variable "doc" can't be returned, and there's also errors in the code. class JsoupParseTask extends AsyncTask<String, Void, Document> { protected Document doInBackground(String... urls) { Document doc = null; try { doc = Jsoup.connect("https://jsoup.org//"

extract language from a web page with Jsoup

你离开我真会死。 提交于 2020-01-24 19:09:37
问题 For example I have <html lang="en"> ...... web page </html> I want to extract the string "en" with Jsoup. I tried with selector and attribute without success. Document htmlDoc = Jsoup.parse(html); Element taglang = htmlDoc.select("html").first(); System.out.println(taglang.text()); 回答1: Looks like you want to get value of lang attribute . In that case you can use attr("nameOfAttribute") like System.out.println(taglang.attr("lang")); 来源: https://stackoverflow.com/questions/29390378/extract

extract language from a web page with Jsoup

久未见 提交于 2020-01-24 19:09:32
问题 For example I have <html lang="en"> ...... web page </html> I want to extract the string "en" with Jsoup. I tried with selector and attribute without success. Document htmlDoc = Jsoup.parse(html); Element taglang = htmlDoc.select("html").first(); System.out.println(taglang.text()); 回答1: Looks like you want to get value of lang attribute . In that case you can use attr("nameOfAttribute") like System.out.println(taglang.attr("lang")); 来源: https://stackoverflow.com/questions/29390378/extract

How to extract any nodes between a node A and a node B with Jsoup?

徘徊边缘 提交于 2020-01-23 18:57:06
问题 I am trying to extract data from a site to construct a database. I want to extract the data from "h2#1" to the line before "h2#2", and put it into Element, so that I can handle the data easier. The data shown in the picture is within a div where id="left" The page I am trying to extract data: http://koryaku.fullbokko.drecom.jp/quests/sp/eiketsu_sinka_no_hihou/netureinokishi/#1 回答1: Try this CSS selector: h2#1 ~ *:not(h2#2 ~ *):not(h2#2) DEMO http://try.jsoup.org/~T29QSXFbJqwJx2a_If4qUeD1cnU

Jsoup解析HTML字符串

蹲街弑〆低调 提交于 2020-01-23 17:55:52
在处理一个html字符串。我们可能需要对其进行解析,修改内容或者提取内容等,那麽我们应该解决这一系列的问题呢?Jsoup可以帮助我们轻松的解决这些问题。 我们可以使用静态 Jsoup.parse(String html) 方法或 Jsoup.parse(String html, String baseUri) 。 String html = "<html><head><title>First parse</title></head>" + "<body><p>Parsed HTML into a doc.</p></body></html>" ; Document doc = Jsoup. parse ( html ) ; 解说: A: parse(String html, String baseUri) 这方法能够将输入的HTML解析为一个新的文档 (Document),参数 baseUri 是用来将相对 URL 转成绝对URL,      并指定从哪个网站获取文档。如这个方法不适用,你可以使用 parse(String html) 方法来解析成HTML字符串如上面的示例。 B: 只要解析的不是空字符串,就能返回一个结构合理的文档,其中包含(至少) 一个head和一个body元素。 C: 一旦拥有了一个Document,你就可以使用Document中适当的方法或它父类

jsoup 简单应用

主宰稳场 提交于 2020-01-22 12:50:20
JSOUP指的是前端爬虫框架,对HTML网页的一系列操作包括信息的获取内容的修改等。 jsoup简单应用 1.三种加载HTML的方法 @Test public void test1() throws IOException { //从URL加载HTML Document document = Jsoup. connect ( "http://www.guge.com" ) . get ( ) ; String title = document. title ( ) ; //获取html中的标题 System.out. println ( "title :" +title ) ; //从字符串加载HTML String html = "<html><head><title>First parse</title></head>" + "<body><p>Parsed HTML into a doc.</p></body></html>" ; Document doc = Jsoup. parse ( html ) ; title = doc. title ( ) ; System.out. println ( "title :" +title ) ; //从文件加载HTML doc = Jsoup. parse ( new File ( "d:\\file\\html\\index

Java爬虫框架 | 爬小说

半腔热情 提交于 2020-01-21 09:35:24
Jsoup,Java爬虫解决方案,中文文档: jsoup 不得不说Java的生态真的好,原来我以为爬虫是只能用Pyhton来写的,结果发现Java的爬虫框架不要太多…… 一分钟你就可以写一个简单爬虫 WebMagic in Action 不过个人觉得Jsoup最好用,最直接也很简单   写了一个Demo,爬取笔趣网的小说,格式已过滤。 public class CrawlText { /*** * 获取文本 * * @param autoDownloadFile * 自动下载文件 * @param Multithreading * 多线程 默认false * @param Url * 网站链接 * @throws IOException */ public static void getText(boolean autoDownloadFile, boolean Multithreading, String Url) throws IOException { String rule = "abs:href"; List<String> urlList = new ArrayList<String>(); Document document = Jsoup.connect(Url) .timeout(4000) .ignoreContentType(true) .userAgent(

webmagic的设计机制及原理-如何开发一个Java爬虫 转

╄→尐↘猪︶ㄣ 提交于 2020-01-19 21:49:17
此文章是webmagic 0.1.0版的设计手册,后续版本的入门及用户手册请看这里: https://github.com/code4craft/webmagic/blob/master/user-manual.md 之前就有网友在博客里留言,觉得webmagic的实现比较有意思,想要借此研究一下爬虫。最近终于集中精力,花了三天时间,终于写完了这篇文章。之前垂直爬虫写了一年多,webmagic框架写了一个多月,这方面倒是有一些心得,希望对读者有帮助。 webmagic的目标 一般来说,一个爬虫包括几个部分: 页面下载 页面下载是一个爬虫的基础。下载页面之后才能进行其他后续操作。 链接提取 一般爬虫都会有一些初始的种子URL,但是这些URL对于爬虫是远远不够的。爬虫在爬页面的时候,需要不断发现新的链接。 URL管理 最基础的URL管理,就是对已经爬过的URL和没有爬的URL做区分,防止重复爬取。 内容分析和持久化 一般来说,我们最终需要的都不是原始的HTML页面。我们需要对爬到的页面进行分析,转化成结构化的数据,并存储下来。 不同的爬虫,对这几部分的要求是不一样的。 <!--more--> 对于通用型的爬虫,例如搜索引擎蜘蛛,需要指对互联网大部分网页无差别进行抓取。这时候难点就在于页面下载和链接管理上--如果要高效的抓取更多页面,就必须进行更快的下载;同时随着链接数量的增多

Parse the inner html tags using jSoup

馋奶兔 提交于 2020-01-17 11:35:15
问题 I want to find the important links in a site using Jsoup library. So for this suppose we have following code: <h1><a href="http://example.com">This is important </a></h1> Now while parsing how can we find that the tag a is inside the h1 tag? 回答1: You can do it this way: File input = new File("/tmp/input.html"); Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/"); Elements headlinesCat1 = doc.getElementsByTag("h1"); for (Element headline : headlinesCat1) { Elements importantLinks