SimpleHttpClientTest
parent
592fa2c0f1
commit
d07941d900
|
@ -0,0 +1,87 @@
|
||||||
|
package us.codecraft.webmagic;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import us.codecraft.webmagic.model.AfterExtractor;
|
||||||
|
import us.codecraft.webmagic.model.annotation.ExtractBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author code4crafter@gmail.com
|
||||||
|
* Date: 2017/6/3
|
||||||
|
* Time: 下午2:54
|
||||||
|
*/
|
||||||
|
public class SimpleHttpClientTest {
|
||||||
|
|
||||||
|
public static class Weather implements AfterExtractor {
|
||||||
|
|
||||||
|
private String location;
|
||||||
|
|
||||||
|
@ExtractBy(notNull = true, value = "//div[@id='7d']//ul[@class='t']/li[2]/p[@class='tem']/i/regex('([\\-\\d]+)',1)")
|
||||||
|
private Integer lowTemperature;
|
||||||
|
|
||||||
|
@ExtractBy(notNull = true, value = "//div[@id='7d']//ul[@class='t']/li[2]/p[@class='tem']/span/regex('([\\-\\d]+)',1)")
|
||||||
|
private Integer highTemperature;
|
||||||
|
|
||||||
|
@ExtractBy(notNull = true, value = "//div[@id='7d']//ul[@class='t']/li[2]/p[@class='wea']/text()")
|
||||||
|
private String desc;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterProcess(Page page) {
|
||||||
|
if (lowTemperature > highTemperature) {
|
||||||
|
int temp = lowTemperature;
|
||||||
|
lowTemperature = highTemperature;
|
||||||
|
highTemperature = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocation(String location) {
|
||||||
|
this.location = location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getLowTemperature() {
|
||||||
|
return lowTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLowTemperature(Integer lowTemperature) {
|
||||||
|
this.lowTemperature = lowTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getHighTemperature() {
|
||||||
|
return highTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHighTemperature(Integer highTemperature) {
|
||||||
|
this.highTemperature = highTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDesc() {
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDesc(String desc) {
|
||||||
|
this.desc = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Weather{" +
|
||||||
|
"location='" + location + '\'' +
|
||||||
|
", lowTemperature=" + lowTemperature +
|
||||||
|
", highTemperature=" + highTemperature +
|
||||||
|
", desc='" + desc + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws Exception {
|
||||||
|
SimpleHttpClient simpleClient = new SimpleHttpClient(Site.me().setUserAgent(
|
||||||
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36"));
|
||||||
|
Weather weather = simpleClient.get("http://www.weather.com.cn/weather/101020100.shtml", Weather.class);
|
||||||
|
System.out.println(weather);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue