我试图将网页的内容作为字符串获取,我发现这个问题解决了如何编写一个基本的网络爬虫 https://stackoverflow.com/questions/1381617/simplest-way-to-correctly-load-html-from-web-page-into-a-string-in-java,它声称(并且似乎)处理编码问题,但是那里提供的适用于美国/英语网站的代码无法正确处理其他语言。
这是一个完整的 Java 类,演示了我所指的内容:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class I18NScraper
{
static
{
System.setProperty("http.agent", "");
}
public static final String IE8_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.2)";
//https://stackoverflow.com/questions/1381617/simplest-way-to-correctly-load-html-from-web-page-into-a-string-in-java
private static final Pattern CHARSET_PATTERN = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
public static String getPageContentsFromURL(String page) throws UnsupportedEncodingException, MalformedURLException, IOException {
Reader r = null;
try {
URL url = new URL(page);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestProperty("User-Agent", IE8_USER_AGENT);
Matcher m = CHARSET_PATTERN.matcher(con.getContentType());
/* If Content-Type doesn't match this pre-conception, choose default and
* hope for the best. */
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
r = new InputStreamReader(con.getInputStream(),charset);
StringBuilder buf = new StringBuilder();
while (true) {
int ch = r.read();
if (ch < 0)
break;
buf.append((char) ch);
}
return buf.toString();
} finally {
if(r != null){
r.close();
}
}
}
private static final Pattern TITLE_PATTERN = Pattern.compile("<title>([^<]*)</title>");
public static String getDesc(String page){
Matcher m = TITLE_PATTERN.matcher(page);
if(m.find())
return m.group(1);
return page.contains("<title>")+"";
}
public static void main(String[] args) throws UnsupportedEncodingException, MalformedURLException, IOException{
System.out.println(getDesc(getPageContentsFromURL("http://yandex.ru/yandsearch?text=%D0%A0%D0%B5%D0%B7%D1%83%D0%BB%D1%8C%D1%82%D0%B0%D1%82%D0%BE%D0%B2&lr=223")));
}
}
哪个输出:
??????????? — ??????: ??????? 360 ??? ???????
虽然它应该是:
Результатов — Яндекс: Нашлось 360 млн ответов
你能帮助我理解我做错了什么吗?尝试强制使用 UTF-8 之类的方法并没有帮助,尽管这是源代码和 HTTP 标头中列出的字符集。
确定正确的字符集编码可能很棘手。
您需要结合使用
a) HTML META Content-Type 标签:
<META http-equiv="Content-Type" content="text/html; charset=EUC-JP">
b) HTTP 响应头:
Content-Type: text/html; charset=utf-8
c) 从字节中检测字符集的启发式方法(参见这个问题 https://stackoverflow.com/questions/499010/java-how-to-determine-the-correct-charset-encoding-of-a-stream)
使用这三个的原因是:
- (a) 和 (b) 可能缺失
- META Content-Type 可能是错误的(请参阅这个问题 https://stackoverflow.com/questions/7545459/html-file-fetched-using-wget-reported-as-binary-by-less)
如果 (a) 和 (b) 都缺失怎么办?
在这种情况下,您需要使用一些启发式方法来确定正确的编码 - 请参阅这个问题 https://stackoverflow.com/questions/499010/java-how-to-determine-the-correct-charset-encoding-of-a-stream.
我发现这个序列对于可靠地识别 HTML 页面的字符集编码来说是最可靠的:
- 使用 HTTP 响应标头 Content-Type(如果存在)
- 对响应内容字节使用编码检测器
- 使用 HTML META 内容类型
但您可能会选择交换 2 和 3。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)