[英]Get page source using Java is reading only 1st line
I have never used java in my life,But i am very good in php , I want to get page source of an website.But i am using Appspot(GAE) .Where file_get_contents and Curl is not working.So i want to get page source via java.I learnt some basics of java and found below code , But below code is getting only 1st line of external page.Please guide me where i am wrong. 我一生中从未使用过Java,但是我非常擅长php,我想获取网站的页面源。但是我正在使用Appspot(GAE),其中file_get_contents和Curl无法正常工作。所以我想获取页面通过java的源代码。我学习了Java的一些基础知识,并在下面的代码中找到了,但是下面的代码仅获得外部页面的第一行。
<?php
function get($url){
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
$java_url = new URL($url);
$java_bufferreader = new BufferedReader(new InputStreamReader($java_url->openStream()));
while (($line = $java_bufferreader->readLine()) != null) {
$content .= $line;
}
return $content;
}
echo get("http://domain.com");
?>
For example , if i scrape stackoverflow.com its returning only below code 例如,如果我刮stackoverflow.com它只返回下面的代码
<!DOCTYPE html><html><head> <title>Stack Overflow</title> <link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico"> <link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png"> <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml"> <meta name="twitter:card" content="summary"> <meta name="twitter:domain" content="stackoverflow.com"/> <meta name="og:type" content="website" /> <meta name="og:image" content="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=fde65a5a78c6"/> <meta name="og:title" content="Stack Overflow" /> <meta name="og:description" content="Q&A for professional and enthusiast programmers" /> <meta name="og:url" content="http://stackoverflow.com/"/>
Try with the Scanner class. 尝试使用Scanner类。
<?php
function get($url){
import java.net.URL;
import java.util.Scanner;
$java_url = new URL($url);
$java_scanner = new Scanner($java_url->openStream());
while (($line = $java_scanner->nextLine()) != null) {
$content .= $line;
}
return $content;
}
echo get("http://domain.com");
?>
If that does not work either, initialize variable content with an empty string, just in case. 如果还是不行,请以空字符串初始化变量内容 ,以防万一。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.