tagname 操作tag
ns|tag ns或tag
#id 用id获得元素
.class 用class获得元素
[attribute] 属性获得元素
[^attr]
: 以attr开头的属性[attr=value] 属性值为
value[attr^=value]
,[attr$=value]
,[attr*=value]
[attr~=regex]正则
*
:所以的标签
选择组合
el#id el和id定位
el.class e1和class定位
el[attr]
e1和属性定位ancestor child
ancestor下面的child
- public void parse(String urlStr) {
- // 返回结果初始化。
- Document doc = null;
- try {
- doc = Jsoup
- .connect(urlStr)
- .userAgent(
- "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)") // 设置User-Agent
- .timeout(5000) // 设置连接超时时间
- .get();
- } catch (MalformedURLException e) {
- log.error( e);
- return ;
- } catch (IOException e) {
- if (e instanceof SocketTimeoutException) {
- log.error( e);
- return ;
- }
- if(e instanceof UnknownHostException){
- log.error(e);
- return ;
- }
- log.error( e);
- return ;
- }
- system.out.println(doc.title());
- Element head = doc.head();
- Elements metas = head.select("meta");
- for (Element meta : metas) {
- String content = meta.attr("content");
- if ("content-type".equalsIgnoreCase(meta.attr("http-equiv"))
- && !StringUtils.startsWith(content, "text/html")) {
- log.debug( urlStr);
- return ;
- }
- if ("description".equalsIgnoreCase(meta.attr("name"))) {
- system.out.println(meta.attr("content"));
- }
- }
- Element body = doc.body();
- for (Element img : body.getElementsByTag("img")) {
- String imageUrl = img.attr("abs:src");//获得绝对路径
- for (String suffix : IMAGE_TYPE_ARRAY) {
- if(imageUrl.indexOf("?")>0){
- imageUrl=imageUrl.substring(0,imageUrl.indexOf("?"));
- }
- if (StringUtils.endsWithIgnoreCase(imageUrl, suffix)) {
- imgSrcs.add(imageUrl);
- break;
- }
- }
- }
- }
这里重点要提的是怎么获得图片或链接的决定地址:
如上获得绝对地址的方法String imageUrl = img.attr("abs:src");//获得绝对路径 ,前面添加abs:jsoup就会获得决定地址;
想知道原因,咱们查看下源码,如下:
- //该方面是先从map中找看是否有该属性key,如果有直接返回,如果没有检查是否
- //以abs:开头
- public String attr(String attributeKey) {
- Validate.notNull(attributeKey);
- if (hasAttr(attributeKey))
- return attributes.get(attributeKey);
- else if (attributeKey.toLowerCase().startsWith("abs:"))
- return absUrl(attributeKey.substring("abs:".length()));
- else return "";
- }
接着查看absUrl方法:
- /**
- * Get an absolute URL from a URL attribute that may be relative (i.e. an <code><a href></code> or
- * <code><img src></code>).
- * <p/>
- * E.g.: <code>String absUrl = linkEl.absUrl("href");</code>
- * <p/>
- * If the attribute value is already absolute (i.e. it starts with a protocol, like
- * <code>http://</code> or <code>https://</code> etc), and it successfully parses as a URL, the attribute is
- * returned directly. Otherwise, it is treated as a URL relative to the element‘s {@link #baseUri}, and made
- * absolute using that.
- * <p/>
- * As an alternate, you can use the {@link #attr} method with the <code>abs:</code> prefix, e.g.:
- * <code>String absUrl = linkEl.attr("abs:href");</code>
- *
- * @param attributeKey The attribute key
- * @return An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or
- * could not be made successfully into a URL.
- * @see #attr
- * @see java.net.URL#URL(java.net.URL, String)
- */
- //看到这里大家应该明白绝对地址是怎么取的了
- public String absUrl(String attributeKey) {
- Validate.notEmpty(attributeKey);
- String relUrl = attr(attributeKey);
- if (!hasAttr(attributeKey)) {
- return ""; // nothing to make absolute with
- } else {
- URL base;
- try {
- try {
- base = new URL(baseUri);
- } catch (MalformedURLException e) {
- // the base is unsuitable, but the attribute may be abs on its own, so try that
- URL abs = new URL(relUrl);
- return abs.toExternalForm();
- }
- // workaround: java resolves ‘//path/file + ?foo‘ to ‘//path/?foo‘, not ‘//path/file?foo‘ as desired
- if (relUrl.startsWith("?"))
- relUrl = base.getPath() + relUrl;
- URL abs = new URL(base, relUrl);
- return abs.toExternalForm();
- } catch (MalformedURLException e) {
- return "";
- }
- }
- }