Java将url读取为字符串并读取所选字符串 [英] Java read url as string and read selected strings

查看:136
本文介绍了Java将url读取为字符串并读取所选字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取网址并选择列表项标签和标题的代码



Code that reads url and selects list items tag and title

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

public class StringUtilitiesFromFile  {

	public static void main(String[] args) throws Exception {		
                 
                String url = "http://www.google.com";             

		String title = StringUtils.substringBetween(url, "<title>", "</title>");
		System.out.println("title:" + title); // good

		String[] tds = StringUtils.substringsBetween(url, "<li>", "</li>");
		for (String td : tds) {
			System.out.println(td); // good
		}		
	}
}





输出为

title:null

线程中的异常mainjava.lang.NullPointerException

at Strings.StringUtilitiesFromFile.main(StringUtilitiesFromFile.java:17)

.... \ ApplicationData \ NetBeans \Cache \8.2 \executor-snippets\run.xml:53:Java返回:1

BUILD FAILED(总时间) :2秒)



我尝试了什么:



更改了网址



缓冲读取器并读取文件



The output is
title:null
Exception in thread "main" java.lang.NullPointerException
at Strings.StringUtilitiesFromFile.main(StringUtilitiesFromFile.java:17)
....\Application Data\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)

What I have tried:

changed url

buffered reader and read as file

推荐答案

引用:

读取网址并选择列表项标签和标题的代码

Code that reads url and selects list items tag and title



否,此代码不读取网址。


No, this code do not read url.

// here you set an arbitrary string named url
String url = "http://www.google.com";
// here you try to find given substrings in string named url
// , not in the page aimed by url
String title = StringUtils.substringBetween(url, "<title>", "</title>");



您没有尝试阅读网址所针对的页面。


You didn't try to read the page aimed by url.


这篇关于Java将url读取为字符串并读取所选字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆