Commit c7330460 authored by yihua.huang's avatar yihua.huang

+sina blog

parent 900172c9
...@@ -1351,6 +1351,7 @@ public class XpathSelectorTest { ...@@ -1351,6 +1351,7 @@ public class XpathSelectorTest {
public void testOschina() { public void testOschina() {
Html html1 = new Html(html); Html html1 = new Html(html);
Assert.assertEquals("再次吐槽easyui", html1.xpath(".//*[@class='QTitle']/h1/a").toString()); Assert.assertEquals("再次吐槽easyui", html1.xpath(".//*[@class='QTitle']/h1/a").toString());
System.out.println(html1.regex("(<body>.*?</body>)").links().toStrings());
} }
} }
package us.codecraft.webmagic.samples; package us.codecraft.webmagic.samples;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Page; 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.processor.PageProcessor;
/** /**
...@@ -15,7 +16,7 @@ public class SinaBlogProcesser implements PageProcessor { ...@@ -15,7 +16,7 @@ public class SinaBlogProcesser implements PageProcessor {
@Override @Override
public void process(Page page) { public void process(Page page) {
page.addTargetRequests(page.getHtml().links().regex("(http://blog\\.sina\\.com\\.cn/s/blog_.*)").toStrings()); page.addTargetRequests(page.getHtml().xpath("//div[@class='articalfrontback SG_j_linedot1 clearfix']").links().toStrings());
page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2")); page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2"));
page.putField("content",page.getHtml().xpath("//div[@id='articlebody']//div[@class='articalContent']")); page.putField("content",page.getHtml().xpath("//div[@id='articlebody']//div[@class='articalContent']"));
page.putField("id",page.getUrl().regex("http://blog\\.sina\\.com\\.cn/s/blog_(\\w+)")); page.putField("id",page.getUrl().regex("http://blog\\.sina\\.com\\.cn/s/blog_(\\w+)"));
...@@ -26,9 +27,13 @@ public class SinaBlogProcesser implements PageProcessor { ...@@ -26,9 +27,13 @@ public class SinaBlogProcesser implements PageProcessor {
@Override @Override
public Site getSite() { public Site getSite() {
if (site==null){ if (site==null){
site = Site.me().setDomain("blog.sina.com.cn").addStartUrl("http://blog.sina.com.cn/flashsword20").setSleepTime(3000). site = Site.me().setDomain("blog.sina.com.cn").addStartUrl("http://blog.sina.com.cn/s/blog_4701280b0102egl0.html").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"); 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");
} }
return site; return site;
} }
public static void main(String[] args) {
Spider.create(new SinaBlogProcesser()).run();
}
} }
package us.codecraft.webmagic.processor; package us.codecraft.webmagic.processor;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import us.codecraft.webmagic.Spider; import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.FilePipeline; import us.codecraft.webmagic.pipeline.FilePipeline;
...@@ -16,6 +17,7 @@ import java.io.IOException; ...@@ -16,6 +17,7 @@ import java.io.IOException;
*/ */
public class DiaoyuwengProcessorTest { public class DiaoyuwengProcessorTest {
@Ignore
@Test @Test
public void test() throws IOException { public void test() throws IOException {
DiaoyuwengProcessor diaoyuwengProcessor = new DiaoyuwengProcessor(); DiaoyuwengProcessor diaoyuwengProcessor = new DiaoyuwengProcessor();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment