日期:2014-05-19 浏览次数:20749 次
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 =&nb