最近有个想法。
最近突然有个想法,我男朋友在淘宝开了一家卖苹果手机系列的店,我喜欢每天晚上12点就去数他今天卖了多少台手机,可是最近生意好了,觉得每天去数数挺麻烦的,所以想写个小程序监听他的历史记录的网页,然后一运行就告诉我几代卖了几只什么的。。。
[最优解释]
这有个前段时间无聊写的查淘宝thinkpad报价的例子,
你改改估计就能用了
public class TestApplet extends JApplet {
private List<Info> infoList = new ArrayList<Info>();
private ContentPanel contentPanel;
@Override
public void init() {
setSize(500, 500);
contentPanel = new ContentPanel();
setContentPane(contentPanel);
}
@Override
public void start() {
initInfo();
}
private void initInfo(){
try {
URL url = new URL(
"http://s.taobao.com/search?q=thinkpad&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&initiative_id=tbindexz_20120920");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String str = null;
StringBuffer sb = new StringBuffer();
String patternStr = "src=[\\w\\W]*?\\<strong\\>.*?\\</strong\\>[^\\n]*?"
+ "\\n[^\\n]*?\\<p class="price"\\>.*?\\</em\\>";
Pattern pattern = Pattern.compile(patternStr);
while((str = reader.readLine()) != null){
sb.append(str + "\n");
System.out.println(str);
}
Matcher matcher = pattern.matcher(sb.toString());
while(matcher.find()){
processString(matcher.group());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
contentPanel.setInfoList(infoList);
}
private void processString(String str){
Pattern namePattern = Pattern.compile("\\<strong\\>(.*?)\\</strong\\>");
Pattern pricePattern = Pattern.compile("\\<em\\>(.*?)\\</em\\>");
Pattern imgPattern = Pattern.compile("src="(.*)"");
String name = "";
String price = "";
String img = "";
Matcher nameMatcher = namePattern.matcher(str);
if(nameMatcher.find()){
name = nameMatcher.group(1);
}
Matcher priceMatcher = pricePattern.matcher(str);
if(priceMatcher.find())
price = priceMatcher.group(1);
Matcher imgMatcher = imgPattern.matcher(str);
if(imgMatcher.find())
img = imgMatcher.group(1);
Info info = new Info(name, price, img);
infoList.add(info);
}
}
class ContentPanel extends JPanel implements ActionListener{
private List<Info> infoList;
private int currentIndex = -1;
private InfoPanel infoPanel;
public ContentPanel(){
init();
}
public void setInfoList(List<Info> infoList){
this.infoList = infoList;
}
private void init(){
setSize(300, 300);
setLayout(null);
infoPanel = new InfoPanel();
add(infoPanel);
JButton preButton = new JButton("<---");
preButton.setBounds(20, 30, 100, 20);
preButton.setActionCommand("pre");
preButton.addActionListener(this);
add(preButton);
JButton nextButton = new JButton("--->");
nextButton.setBounds(380, 30, 100, 20);
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("pre")){
if(currentIndex > 0){
currentIndex --;
infoPanel.setInfo(infoList.get(currentIndex));
}
}else if(cmd.equals("next")){
if(currentIndex < infoList.size() - 1){
currentIndex ++;
infoPanel.setInfo(infoList.get(currentIndex));
}
}
}
}
class InfoPanel extends JPanel{
private Info info;
private BufferedImage img;
public InfoPanel(){
setBounds(100, 100, 300, 300);
}
public void setInfo(Info info){
this.info = info;
try {
URL imgUrl = new URL(info.getImg());
img = ImageIO.read(imgUrl.openStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
if(img != null){
g2.drawImage(img, 0, 0, this);
}
if(info != null){
g2.drawString(info.getName() + ":" + info.getPrice(), 10, 200);
}
}
}
class Info{
private String name;
private String price;
private String img;
public Info(String name,String price,String img){
this.name = name;
this.price = price;
this.img = img;
}
public String getName() {
return name;
}
public String getPrice() {
return price;
}
public String getImg() {
return img;
}
}