java获取url网页指定内容
2013-08-07 13:11
375 查看
在获取网页源代码(←获取html网页源代码请看这儿)的前提下,根据网页源代码获取指定内容
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @use 获取指定HTML标签的相关内容 */ public class GetTheSpecifyHtmlTagCode { /** * 获取指定HTML标签的代码及指定属性的值 * * @param source 要匹配的源文本 * @param element 标签名称 * @param byAttr 标签的属性名称 * @return 属性值列表 */ public static List<String> match(String source, String element, String byAttr) { List<String> result = new ArrayList<String>(); String reg = String.format("<%s[^<>]*?\\s?%s=['\"](.*?)['\"]\\s?.*?>(.*?)</%s>", element, byAttr, element); Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { String elementResult = m.group(0); String attrResult = m.group(1); result.add(elementResult + VS + attrResult); } return result; } /** * 通过属性名称获取属性值 * * @param source 要匹配的源文本 * @param element 标签名称 * @param byAttr 标签的属性名称 * @return 属性值 */ public static String getAttrValueByAttr(String source, String element, String byAttr) { String result = ""; String reg = String.format("<%s[^<>]*?\\s?%s=['\"](.*?)['\"]\\s?.*?>(.*?)</%s>", element, byAttr, element); Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { result = m.group(1); } return result; } /** * 根据指定的属性值获取指定的其他属性值 * * @param htmlCode 网页源代码 * @param element 标签名称 * @param byAttr1 指定的属性名称 * @param attrValue 指定的属性值 * @param byAttr2 指定的其他属性名称 * @return 指定的其他属性值 */ public static String getAttrValue(String htmlCode, String element, String byAttr1, String attrValue, String byAttr2) { List<String> list = match(htmlCode, element, byAttr1); String tempr = ""; for (int i = 0; i < list.size(); i++) { String tempResult = list.get(i); String[] temp = tempResult .split(VS); if (temp[1].equals(attrValue)) { tempr = getAttrValueByAttr(htmlCode, element, byAttr2); } } return tempr; } /** * 根据指定属性获取指定html标签的代码 * * @param htmlCode 网页源代码 * @param element 标签名称 * @param byAttr 属性名称 * @param attrValue 属性值 * @return 指定html标签的代码 */ public static String getElementById(String htmlCode, String element, String byAttr, String attrValue) { List<String> list = match(htmlCode, element, byAttr); String tempr = ""; for (int i = 0; i < list.size(); i++) { String tempResult = list.get(i); String[] temp = tempResult .split(VS); // System.out.println("--------> " + (i + 1) + ". " + list.get(i)); if (temp[1].equals(attrValue)) { // System.out.println("内部打印 " + temp[0]); tempr = temp[0]; } } return tempr; } /** * 获取指定标签包含的内容 * * @param htmlCode 网页源代码 * @param element 标签名称 * @param byAttr 属性 * @param attrValue 属性值 * @return 指定标签包含的内容 */ public static String getElementValueByAttr(String htmlCode, String element, String byAttr, String attrValue) { String elementCode = getElementById(htmlCode, element, byAttr, attrValue); String regTagStart = String.format("<%s[^<>]*?\\s?%s=['\"](.*?)['\"]\\s?.*?>", element,byAttr); String tagStart=""; Matcher m = Pattern.compile(regTagStart).matcher(elementCode); while (m.find()) { tagStart = m.group(0); } String regTagEnd = String.format("</%s>", element); String result = elementCode.replace(tagStart, "").replace(regTagEnd, ""); return result; } /** * 根据已有标签代码获取标签包含的内容 * * @param elementCode 标签代码 * @param element 标签名称 * @return 标签包含的内容 */ public static String getElementValueByElementCode(String elementCode, String element,String byAttr) { String regTagStart = String.format("<%s[^<>]*?\\s?%s=['\"](.*?)['\"]\\s?.*?>", element,byAttr); String tagStart=""; Matcher m = Pattern.compile(regTagStart).matcher(elementCode); while (m.find()) { tagStart = m.group(0); } String regTagEnd = String.format("</%s>", element); String result = elementCode.replace(tagStart, "").replace(regTagEnd, ""); return result; } private static String VS = "--------";//split标记自定义 }
相关文章推荐
- java通过url获取网页内容
- JAVA HttpURLConnection 获取网页内容
- Java 抓取网页内容,获取指定服务器IP
- JAVA HttpURLConnection 获取网页内容
- java-抓取指定URL网页的内容
- Java 从网页指定URL获取文件
- java 获取 网页中 指定的 内容,老牛比, 赶紧的
- java读取URL指定的网页内容
- java中通过url获取网页内容,中文显示是乱码
- java用url获取网页内容并输出到控制台
- Java使用URL获取网页内容
- 用java 获取指定url 网站内容
- java-抓取指定URL网页的内容(二)
- Android 获取指定url网页内容
- java 获取网页指定内容
- 用java 获取指定url 网站内容
- 通过URL对象获取指定网页的内容
- 使用JAVA获取指定网页内容
- java 获取网页指定内容-2(实践+修改)
- Java 抓取网页内容,获取指定服务器IP