Java:如何使用JSoup从该网站读取数据?我如何找到标签和类之类的东西?
我试图从这个网站的每一列中读取口袋妖怪的名称和坐标,以开发一个桌面应用程序来显示这些数据。我从未用HTML编写过程序 这对我来说是如此的新鲜和复杂,我已经搜索了很多教程,但它们从未解释如何获得元素ID、标记等来指定它们想要打印的数据。我知道web页面上的inspect元素,但我不知道要查找什么,所以如果您能帮我制作一个示例,打印出数据,我可以对其进行分析,以了解如何使用JSoup,我将不胜感激 或者,如果你觉得很慷慨,你也可以解释一下你是如何得到这些标签和ID的,这样我可以更快地了解这些信息,以备将来参考 Jsoup是一个HTML解析器,它的“类似jquery”和“regex”选择器语法是 非常容易使用,足够灵活,可以得到你想要的任何东西。在下面 下面是三个示例,演示如何使用Jsoup获取链接、图像、, HTML页面中的页面标题和“div”元素内容 基本上,只有在需要进行大量编辑时才应该使用jsoup。比如,如果你想给许多标签赋予id,或者如果你想从互联网网页上读取数据 这个来自jsoup官方网站的例子应该会有所帮助。 这个示例程序演示了如何从URL获取页面; 提取链接、图像和其他指针;并检查它们的URL和 文本 指定要获取的URL作为程序的唯一参数 输出 Jsoup是一个HTML解析器,它的“类似jquery”和“regex”选择器语法是 非常容易使用,足够灵活,可以得到你想要的任何东西。在下面 下面是三个示例,演示如何使用Jsoup获取链接、图像、, HTML页面中的页面标题和“div”元素内容 基本上,只有在需要进行大量编辑时才应该使用jsoup。比如,如果你想给许多标签赋予id,或者如果你想从互联网网页上读取数据 这个来自jsoup官方网站的例子应该会有所帮助。 这个示例程序演示了如何从URL获取页面; 提取链接、图像和其他指针;并检查它们的URL和 文本 指定要获取的URL作为程序的唯一参数 输出Java:如何使用JSoup从该网站读取数据?我如何找到标签和类之类的东西?,java,html,web-scraping,jsoup,Java,Html,Web Scraping,Jsoup,我试图从这个网站的每一列中读取口袋妖怪的名称和坐标,以开发一个桌面应用程序来显示这些数据。我从未用HTML编写过程序 这对我来说是如此的新鲜和复杂,我已经搜索了很多教程,但它们从未解释如何获得元素ID、标记等来指定它们想要打印的数据。我知道web页面上的inspect元素,但我不知道要查找什么,所以如果您能帮我制作一个示例,打印出数据,我可以对其进行分析,以了解如何使用JSoup,我将不胜感激 或者,如果你觉得很慷慨,你也可以解释一下你是如何得到这些标签和ID的,这样我可以更快地了解这些信息,以
package org.jsoup.examples;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
/**
* Example program to list links from a URL.
*/
public class ListLinks {
public static void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
print("Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
Elements media = doc.select("[src]");
Elements imports = doc.select("link[href]");
print("\nMedia: (%d)", media.size());
for (Element src : media) {
if (src.tagName().equals("img"))
print(" * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20));
else
print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
}
print("\nImports: (%d)", imports.size());
for (Element link : imports) {
print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
}
print("\nLinks: (%d)", links.size());
for (Element link : links) {
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}
private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
}
Fetching http://news.ycombinator.com/...
Media: (38)
* img: <http://ycombinator.com/images/y18.gif> 18x18 ()
* img: <http://ycombinator.com/images/s.gif> 10x1 ()
* img: <http://ycombinator.com/images/grayarrow.gif> x ()
* img: <http://ycombinator.com/images/s.gif> 0x10 ()
* script: <http://www.co2stats.com/propres.php?s=1138>
* img: <http://ycombinator.com/images/s.gif> 15x1 ()
* img: <http://ycombinator.com/images/hnsearch.png> x ()
* img: <http://ycombinator.com/images/s.gif> 25x1 ()
* img: <http://mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.)
Imports: (2)
* link <http://ycombinator.com/news.css> (stylesheet)
* link <http://ycombinator.com/favicon.ico> (shortcut icon)
Links: (141)
* a: <http://ycombinator.com> ()
* a: <http://news.ycombinator.com/news> (Hacker News)
* a: <http://news.ycombinator.com/newest> (new)
* a: <http://news.ycombinator.com/newcomments> (comments)
* a: <http://news.ycombinator.com/leaders> (leaders)
* a: <http://news.ycombinator.com/jobs> (jobs)
* a: <http://news.ycombinator.com/submit> (submit)
* a: <http://news.ycombinator.com/x?fnid=JKhQjfU7gW> (login)
* a: <http://news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73> ()
* a: <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter> (Facebook speeds up PHP)
* a: <http://news.ycombinator.com/user?id=mcxx> (mcxx)
* a: <http://news.ycombinator.com/item?id=1094578> (9 comments)
* a: <http://news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73> ()
* a: <http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914> ("Tough. Django produces XHTML.")
* a: <http://news.ycombinator.com/user?id=andybak> (andybak)
* a: <http://news.ycombinator.com/item?id=1094649> (3 comments)
* a: <http://news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73> ()
* a: <http://news.ycombinator.com/x?fnid=p2sdPLE7Ce> (More)
* a: <http://news.ycombinator.com/lists> (Lists)
* a: <http://news.ycombinator.com/rss> (RSS)
* a: <http://ycombinator.com/bookmarklet.html> (Bookmarklet)
* a: <http://ycombinator.com/newsguidelines.html> (Guidelines)
* a: <http://ycombinator.com/newsfaq.html> (FAQ)
* a: <http://ycombinator.com/newsnews.html> (News News)
* a: <http://news.ycombinator.com/item?id=363> (Feature Requests)
* a: <http://ycombinator.com> (Y Combinator)
* a: <http://ycombinator.com/w2010.html> (Apply)
* a: <http://ycombinator.com/lib.html> (Library)
* a: <http://www.webmynd.com/html/hackernews.html> ()
* a: <http://mixpanel.com/?from=yc> ()