【发布时间】:2016-01-31 09:19:30
【问题描述】:
我正在与 URL's 合作,更准确地说是在 Stack Overflow 上。
网站URLs 的questions 部分的结构是:
/questions/tagged/tag+anotherTag+lastTag
在尝试使用URL 时,我只收到第一个标签的问题。
示例
URL url = null;
InputStream is = null;
BufferedReader br;
String line;
try{
url = new URL("https://stackoverflow.com/questions/tagged/cobol+hibernate");
br = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = br.readLine()) != null) {
if (line.contains("<div class=\"tags")){
System.out.println(line);
}
}
} catch (Exception e){
e.printStackTrace();
}
System.out.println(url);
输出
<div class="tags t-cobol">
<div class="tags t-batch-file t-cobol t-mainframe t-vsam">
<div class="tags t-cobol t-mainframe">
<div class="tags t-cobol t-opencobol t-microfocus">
<div class="tags t-cobol">
https://stackoverflow.com/questions/tagged/cobol+hibernate
预期输出
// Nothing because there is no question under both tags
https://stackoverflow.com/questions/tagged/cobol+hibernate
实际链接是一个empty page(以这种方式,从来没有将任何问题与两个标签一起发布),正如您所见,代码只查找用第一个标签标识的问题。
Cobol+Hibernate只是一个很好解释问题的例子,我知道把这两个标签放在一起是没有逻辑的。
【问题讨论】:
-
"实际链接是一个空页面,如您所见,代码仅查找由第一个标签标识的问题。"有趣的是,“空白页面”似乎为我搜索带有两个标签的问题。
-
@Powerlord Empty,因为它不包含任何包含两个标签的问题...
-
这是因为加号充当 AND 运算符。它正在搜索包含这两个标签的项目。尝试使用 java+swing 之类的标签,它工作得很好。它给你一个空白页面,因为没有人用 cobol 和 hibernate 作为标签提出问题。
-
该页面以 UTF-8 编码。您对
new InputStreamReader(url.openStream())的使用将使用您自己机器的默认字符集读取页面的字节。您的默认字符集是 UTF-8 吗? (如果您在 Windows 上运行,答案是否定的。) -
@ChaseHenslee 这有点不直观,因为搜索非标签是通过查询字符串完成的。 + 用于替换查询字符串中的空格。
标签: java url inputstream