Directory index = SimpleFSDirectory.open(new File("d:/20101015index"));
package us.codecraft.webmagic.samples;
import java.util.List;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
/**
* @author code4crafter@gmail.com <br>
*/
public class SinaBlogProcessor implements PageProcessor {
public static final String URL_LIST = "http://blog\\.sina\\.com\\.cn/s/articlelist_1487828712_0_\\d+\\.html";
public static final String URL_POST = "http://blog\\.sina\\.com\\.cn/s/blog_\\w+\\.html";
private Site site = Site
.me()
.setDomain("blog.sina.com.cn")
.setSleepTime(3000)
.setUserAgent(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
@Override
public void process(Page page) {
System.out.println(" html: "+page.getUrl());
//if (page.getUrl().toString().indexOf("mainpage.html") >= 0)
if(false){
List<String> elems = page.getHtml().xpath("//div[@class=\"boutlist\"]/ul/li")
.all();
for (String elem : elems)
{
//System.out.println(elem);
}
//List<String> elems = page.getHtml().xpath("//div[@class=\"ss1\"]").all(); // links().all();
int i = 0;
for (String elem : elems) {
// System.out.println(elem);
Html pg = new Html(elem);
System.out.println("\r\n\r\n");
Html td0 = new Html(pg.xpath("//div[@class=\"tdtitle\"]").all()
.get(0));
String url = td0.xpath("//a/@href").toString();
String candy = td0.xpath("//a/@title").toString();
String title = td0.xpath("//a/text()").toString();
String ename = td0.xpath("//div[@class=\"tdtitle\"]/text()")
.toString();
ename = ename.substring(2);
// System.out.println(elem);
Html td1 = new Html(pg.xpath("//div[@class=\"tdtitle\"]").all()
.get(1));
String info = td1.xpath("//div[@class=\"tdtitle\"]/text()")
.toString();
String picurl = pg.xpath("//div[@class=\"tdimg\"]/a/img/@src")
.toString();
String dbfen = pg.xpath("//div[@class=\"ss5\"]/p/font/text()").all().get(0).toString();
String imdbfen = pg.xpath("//div[@class=\"ss5\"]/p/font/text()").all().get(1).toString();
System.out.println(i + ":" + title + " " + ename + " " + candy
+ " " + url + " " + info + " " + picurl+ " "+dbfen+" "+imdbfen);
i++;
}
}else
{
String title = page.getHtml().xpath("//div[@class=\"info\"]/h1/text()").all().get(0).toString();
String info0 = page.getHtml().xpath("//div[@class=\"info\"]/ul/li").all().get(0).toString();
String info1 = page.getHtml().xpath("//div[@class=\"info\"]/ul/li").all().get(1).toString();
String info2 = page.getHtml().xpath("//div[@class=\"info\"]/ul/li").all().get(2).toString();
String info3 = page.getHtml().xpath("//div[@class=\"info\"]/ul/li").all().get(3).toString();
String imgurl = page.getHtml().xpath("//div[@class=\"pic\"]/img/@src").all().get(0).toString();
String juqing = page.getHtml().xpath("//div[@id=\"endtext\"]/text()").all().get(0).toString();
//关于容错
List<String> imgs = page.getHtml().xpath("//div[@id=\"endtext\"]/img/@src").all();
System.out.println(" "+title+", "+info0+", "+info1+", "+info2+", "+imgurl+" \r\n: "+juqing);
for(String img:imgs)
{
System.out.println(" "+img);
}
}
/*
//列表页
if (page.getUrl().regex(URL_LIST).match()) {
page.addTargetRequests(page.getHtml().xpath("//div[@class=\"articleList\"]").links().regex(URL_POST).all());
page.addTargetRequests(page.getHtml().links().regex(URL_LIST).all());
//文章页
} else {
page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2"));
page.putField("content", page.getHtml().xpath("//div[@id='articlebody']//div[@class='articalContent']"));
page.putField("date",
page.getHtml().xpath("//div[@id='articlebody']//span[@class='time SG_txtc']").regex("\\((.*)\\)"));
}
*/
}
@Override
public Site getSite() {
return site;
}
public static void main(String[] args) {
//Spider.create(new SinaBlogProcessor()).addUrl("http://127.0.0.1:8080/test/mainpage.html").run();
Spider.create(new SinaBlogProcessor()).addUrl("http://127.0.0.1:8080/test/page.html").run();
}
}
package jsoup;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class test1 {
public static void main(String[] args) throws IOException {
URL url = new URL( "http://localhost:8080/test/mainpage_files/p2375744186.jpg");
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
File file = new File("d://1.jpg");
FileOutputStream out = new FileOutputStream(file);
int i=0;
while ((i=is.read())!=-1) {
out.write(i);
}
is.close();
System.out.println("ok");
}
}
<ul class="list-group">
<li class=" list-group-item list-group-item-success"><span class="badge">14</span>
<div class="rowmain" >
<tbody>
<tr>
<td class="tdimg">
<img class="listimg" src="../static/images/p2371145119.jpg" style="display: inline;">
</img>
</td>
<td >
<div class="p12" style="display: inline;">
<tr>
<a style="display: inline;">七月与安生</a>
</tr>
<tr>
<p >asfafd</p></tr>
</div>
</td>
</tr></tbody>
<div>
</li>
</ul>