必须的,如果你跳过他的拦截器,直接访问它的页面,那就不安全了,在设计的时候就是这么设计的
成都创新互联公司专注于龙门企业网站建设,响应式网站,成都做商城网站。龙门网站建设公司,为龙门等地区提供建站服务。全流程按需网站制作,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
如果评论是通过AJAX显示的,那么抓取有一定难度。
你的爬虫需要能够解释JS,并解惑JS的内容。
但如果你只针对少数的网站进行抓取,则可以针对这些网站开发专用的蜘蛛。人工分析其JS,从中找到其获取评论的AJAX接口,然后抓之。这样简单。
还可以用爬虫操作一个浏览器,通过浏览器的接口获取其运行完成后的显示的内容
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java点虐 .HttpURLConnection;
import java点虐 .MalformedURLException;
import java点虐 .URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class HttpViewer extends JFrame {
private JTextField urlInput;
private JTextArea viewArea;
public static void main(String[] args) {
new HttpViewer();
}
public HttpViewer() {
this.setTitle("Http Viewer");
this.setSize(800, 600);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initPanel();
initAction();
this.setVisible(true);
}
// 这个方法用来设置窗口布局
private void initPanel() {
JPanel northPanel = new JPanel();
JLabel urlInputLabel = new JLabel("URL:");
urlInput = new JTextField(60);
northPanel.add(urlInputLabel);
northPanel.add(urlInput);
this.add(northPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
viewArea = new JTextArea(27, 60);
centerPanel.add(new JScrollPane(viewArea));
this.add(centerPanel);
}
// 这个方法用来设置事件
private void initAction() {
urlInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = urlInput.getText();
if (text == null || text.length() == 0) {
viewArea.setText("您没有输入URL");
return;
}
try {
URL url = new URL(text);
String context = getContent(url);
if (context != null) {
searchFromText(context);
}
} catch (MalformedURLException e1) {
viewArea.setText("您输入的URL不合法:" + text);
}
}
});
}
private String getContent(URL url) {
StringBuffer builder = new StringBuffer();
int responseCode = -1;
HttpURLConnection con = null;
try {
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理进行下载
con.setConnectTimeout(60000);
con.setReadTimeout(60000);
// 获得网页返回信息码
responseCode = con.getResponseCode();
if (responseCode == -1) {
viewArea.setText("连接失败:" + url.toString());
return null;
}
if (responseCode = 400) {
viewArea.setText("请求失败,错误码:" + responseCode);
return null;
}
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String str = null;
while ((str = br.readLine()) != null)
builder.append(str);
is.close();
} catch (IOException e) {
e.printStackTrace();
viewArea.setText("IOException: " + url.toString());
} finally {
con.disconnect();
}
return builder.toString();
}
private void searchFromText(String context) {
viewArea.setText("查找URL中:\n");
Pattern pattern = Pattern点抗 pile("a( [^]+)*(.*?)/a");
Matcher matcher = pattern.matcher(context);
while (matcher.find()) {
for (String prop : matcher.group(1).split(" ")) {
int indexOf = prop.indexOf('=');
if (indexOf 0) {
if (prop.substring(0, indexOf).equals("href")) {
String url2 = prop.substring(indexOf + 2, prop.length() - 1);
viewArea.append(url2 + "\n");
}
}
}
}
}
}