我试图从一个特定的URL读取HTML并将其存储到一个字符串中进行解析。我找了一个previous post来帮我。当我把读到的东西打印出来时,我得到的只是一些特殊的字符。你知道吗
下面是我的Java代码(省略try/catch),它从URL读取并打印:
String path = "https://html1-f.scribdassets.com/913q5pjrsw60h9i4/pages/106-6b1bd15200.jsonp";
URL url = new URL(path);
InputStream in = url.openStream();
BufferedReader bw = new BufferedReader(new InputStreamReader(in, "UTF-8");
String line;
while ((line = bw.readLine()) != null) {
System.out.println(line);
}
程序输出:
�ĘY106-6b1bd15200.jsonpmP�r� �Ƨ�!�%m�vD"��Ra*��w�%����ݳ�sβ��MK�d�9+%�m��l^��މ����:���� ���8B�Vce�.A*��x$FCo���a�b�<����Xy��m�c�>t����� �Z������Gx�o� �J���oKe�0�5�kGYpb�*l����+|�U���-�N3��jBp�R�z5Cۥjh��o�;�~)����~��)~ɮhy��<c,=;tHW���'�c�=~�w���
预期产量:
window.page106_callback(["<div class=\"newpage\" id=\"page106\" style=\"width: 902px; height:1273px\">\n<div class=image_layer style=\"z-index: 1\">\n<div class=ie_fix>\n<img class=\"absimg\" style=\"left:18px;top:27px;width:860px;height:1077px;clip:rect(1px 859px 1076px 1px)\" orig=\"http://html.scribd.com/913q5pjrsw60h9i4/images/106-6b1bd15200.jpg\"/>\n</div>\n</div>\n</div>\n\n"]);
一开始,我认为这是权限的问题,或者是某种加密流的东西,但是我的朋友写了一个小Python脚本来做同样的事情,它成功了,从而排除了这种可能性。他写道:
import requests
link = 'https://html1-f.scribdassets.com/913q5pjrsw60h9i4/pages/106-
6b1bd15200.jsonp'
f = requests.get(link)
text = (f.text)
print(text)
所以问题是,为什么Java版本不能正确读取和打印这个特定的URL?请注意,我尝试测试了来自不同网站的其他一些URL,这些URL运行良好。也许我应该学Python。你知道吗
@Maurice Perry是对的,我试过下面的代码
响应是gzip编码的。你可以做:
相关问题 更多 >
编程相关推荐