这样不知道行不行 package ex30;import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*;public class ViewRemoteFile extends JApplet{ // Button to view the file private JButton jbtView = new JButton("View");
// Text field to receive file name private JTextField jtfURL = new JTextField(12);
// Text area to store file private JTextArea jtaFile = new JTextArea();
// Label to display status private JLabel jlblStatus = new JLabel();
/** Initialize the applet */ public void init(){ // Create a panel to hold a label, a text field, and a button JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout()); p1.add(new JLabel("Filename"), BorderLayout.WEST); p1.add(jtfURL, BorderLayout.CENTER); p1.add(jbtView, BorderLayout.EAST);
// Place text area and panel p to the applet setLayout(new BorderLayout()); add(new JScrollPane(jtaFile), BorderLayout.CENTER); add(p1, BorderLayout.NORTH); add(jlblStatus, BorderLayout.SOUTH);
// Register listener to handle the "View" button jbtView.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ showFile(); } }); }
private void showFile(){ java.util.Scanner input = null; // Use Scanner for text input URL url = null;
try{ // Obtain URL from the text field url = new URL(jtfURL.getText().trim());
// Create a Scanner for input stream input = new java.util.Scanner(url.openStream());
// Read a line and append the line to the text area while(input.hasNext()){ jtaFile.append(input.nextLine() + "\n"); }
package ex30;import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;public class ViewRemoteFile extends JApplet{
// Button to view the file
private JButton jbtView = new JButton("View");
// Text field to receive file name
private JTextField jtfURL = new JTextField(12);
// Text area to store file
private JTextArea jtaFile = new JTextArea();
// Label to display status
private JLabel jlblStatus = new JLabel();
/** Initialize the applet */
public void init(){
// Create a panel to hold a label, a text field, and a button
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(new JLabel("Filename"), BorderLayout.WEST);
p1.add(jtfURL, BorderLayout.CENTER);
p1.add(jbtView, BorderLayout.EAST);
// Place text area and panel p to the applet
setLayout(new BorderLayout());
add(new JScrollPane(jtaFile), BorderLayout.CENTER);
add(p1, BorderLayout.NORTH);
add(jlblStatus, BorderLayout.SOUTH);
// Register listener to handle the "View" button
jbtView.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showFile();
}
});
}
private void showFile(){
java.util.Scanner input = null; // Use Scanner for text input
URL url = null;
try{
// Obtain URL from the text field
url = new URL(jtfURL.getText().trim());
// Create a Scanner for input stream
input = new java.util.Scanner(url.openStream());
// Read a line and append the line to the text area
while(input.hasNext()){
jtaFile.append(input.nextLine() + "\n");
}
jlblStatus.setText("File loaded successfully");
}
catch(MalformedURLException ex){
jlblStatus.setText("URL " + url + " not found");
}
catch(IOException e){
jlblStatus.setText(e.getMessage());
}
finally{
if(input != null) input.close();
}
}
}输入网址是要http://www.baidu.com这样的形式
不知道有没有其他的可替代方案,如:通过java的脚本执行引擎执行js来获取网页代码,或者其他的方案。请各位大侠帮忙,感激不尽!好像没办法实现吧,js执行过程需要构建运行环境才能实现。例如页面引用了jquery、extjs这些东西,js运行后必须要修改了dom的结构的。
除非你自己写个js引擎,呵呵
URL url = new URL("http://www.baidu.com");
BufferedReader buff = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb = new StringBuilder();
String s = null;
while((s = buff.readLine()) != null){
sb.append(s+"\n");
}
System.out.println(sb);
这种方式跟5楼的一样结果。
不知道楼主现在项目还有没有做出来。
还有就是只能抓取当前页面中有的东西。没有的东西是不能抓取到的。还有抓取时好听的。个人觉得应该是读取。IO流了。
* 读取url地址,封装一个新闻实体对象
* @param url
* @param title 新闻标题
* @return
*/
public News readNews(String url, String title) { News news = null; try {
//jsoup读取html
org.jsoup.nodes.Document doc = Jsoup.parse(new URL(url), 2000); //body
Element content = doc.body();
//所有的h1 node
Elements links = content.getElementsByTag("h1");
String html = null;
if (links.isEmpty()) {
return null;
} Element e = null;
for (Element link : links) {
//如果该node的文本节点包含新闻标题title
if (link.text().contains(title)) {
e = link;
break;
}
} if (e != null) {
html = e.parent().toString();
news = new News();
news.setTitle(title);
news.setContent(html);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return news; }
jsoup能很方便的读取和操作html,就想jquery一样。希望能给与楼主帮助。
首先你要获取请求的地址,这个可以利用谷歌浏览器查看一下,然后用HttpClient模拟请求那个地址就可以了。我之前做爬虫爬取新浪微博就是这样做的。
纠正一下你的观点哈,不是所有js都要请求服务器的,如:document.getElementById("content").innerHTML="content";这个就不会去请求服务器。
谁知道使用java调用webkit的方式(非android平台那种调用哈)呢?
<script type="text/javascript">
document.ready(function(){
$.ajax({
......
success: function(data){
$("#div1").html(data);
}
});
});
</script>
<div id="div1">
</div>
那么你的搜索排名永远都无法靠前