【发布时间】:2012-08-17 15:03:25
【问题描述】:
我是 Java 初学者,目前我正在开发一个 SWING JAVA 应用程序。
这个应用程序是做什么的。做。
如果用户提示一些数字(比如说)[输入],那么应用程序。连接到特定网站并在那里搜索(网址:http://www.example.com/search?text=[user's input])。输出(我想要)将是是否可以找到用户的输入(借助布尔真/假和 url 连接)。就是这个。
我已经阅读了A java program to search in a certain website,但我不想使用爬虫。对我来说似乎很难。 另外,我认为,我不需要解析网站,因为如果用户的输入存在,它将返回用户正在寻找的内容。所以不需要像jsoup这样的程序。
我的问题是:我应该用什么肉。如何解决这个问题呢? 基本上,如何将用户的输入放入网站 URL,然后单击摆动按钮在该网站上进行搜索
编辑: 这么几点。
- 用户输入应该返回肯定的结果,例如 this e.g. “example.com/descriptionOfProduct.k973311”,其中 97311 是该产品的特定 ID。它总是有 6 个数字。还有字母“k”。是不是也一直在。
- 如果用户输入不存在,则 404。如果存在,则参见上文。而且 descriptionOfProduct 总是完全不同的。而且这绝对是随机的。
我使用 httpclient apache。
关于该特定网站:没有用于搜索的 api,它是电子商务 (eshop)。
这是我的代码,由于这个搜索功能,它没有完成。
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
public class Connection {
/**
* @param args
* @throws URISyntaxException
*/
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws URISyntaxException {
HttpURLConnection connection = null;
try {
URL url = new URL("http://www.example.cz");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
connection.getInputStream();
System.out.println("Connected to website");
// do something with the input stream here
// InputStream error = ((HttpURLConnection) connection).getErrorStream();
// # means special ID to understand where & what is wrong
} catch (MalformedURLException e1) {
e1.printStackTrace();
System.err.println("Something's wrong #1");
} catch (IOException e1) {
e1.printStackTrace();
System.err.println("Something's wrong #2");
} finally {
if(null != connection) { connection.disconnect(); }
}
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.example.com").setPath("/search")
.setParameter("text", "");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
这是摇摆应用程序
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
/**
* @author John Malc
* @version
*
*/
public class app {
private JFrame frame;
private JTextField textField;
private JButton btnSearch;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
app window = new app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public app() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(getTextField(), BorderLayout.NORTH);
frame.getContentPane().add(getBtnSearch(), BorderLayout.SOUTH);
}
private JTextField getTextField() {
if (textField == null) {
textField = new JTextField();
textField.setColumns(10);
}
return textField;
}
private JButton getBtnSearch() {
if (btnSearch == null) {
btnSearch = new JButton("search");
}
return btnSearch;
}
}
【问题讨论】:
-
“是否可以找到用户的输入”是什么意思?您的意思是他们输入的术语出现在页面example.com 的某处吗?或者你的意思是example.com/search?text=[user-input] 实际上返回了某种积极的结果?成功后是否会返回一个关键短语?
-
我的意思是 example.com/search?text=[user-input] 返回一个肯定的结果(在这种情况下它将直接是某个产品)
-
那么成功的标准是什么?是否有一些关键短语或其他指标表明该页面是积极的结果?也许它只是“未找到结果”之外的其他内容,或者您检查主要内容
<div>中有多行带有图像的? -
是的,也许是这个。在 URL 中,您将始终获得“example.com/descriptionOfProduct.k973311 {该产品的特定 ID,6 个数字;也可能是 .kxxxxxx}。
-
因此请检查您的
HttpResponse中是否包含descriptionOfProduct。够了吗?我假设如果它没有找到任何东西,那么会有一个不同的 URL not 包含descriptionOfProduct。