fix ZipCodePageProcessor
parent
2801daf185
commit
9606a173cd
|
@ -109,7 +109,7 @@ public class PlainText implements Selectable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String get() {
|
||||||
if (CollectionUtils.isNotEmpty(all())) {
|
if (CollectionUtils.isNotEmpty(all())) {
|
||||||
return all().get(0);
|
return all().get(0);
|
||||||
} else {
|
} else {
|
||||||
|
@ -117,6 +117,11 @@ public class PlainText implements Selectable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean match() {
|
public boolean match() {
|
||||||
return strings != null && strings.size() > 0;
|
return strings != null && strings.size() > 0;
|
||||||
|
|
|
@ -99,6 +99,13 @@ public interface Selectable {
|
||||||
*/
|
*/
|
||||||
public String toString();
|
public String toString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* single string result
|
||||||
|
*
|
||||||
|
* @return single string result
|
||||||
|
*/
|
||||||
|
public String get();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* if result exist for select
|
* if result exist for select
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,8 +9,9 @@ import us.codecraft.webmagic.processor.PageProcessor;
|
||||||
import us.codecraft.webmagic.scheduler.PriorityScheduler;
|
import us.codecraft.webmagic.scheduler.PriorityScheduler;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static us.codecraft.webmagic.selector.Selectors.regex;
|
|
||||||
import static us.codecraft.webmagic.selector.Selectors.xpath;
|
import static us.codecraft.webmagic.selector.Selectors.xpath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,16 +20,16 @@ import static us.codecraft.webmagic.selector.Selectors.xpath;
|
||||||
public class ZipCodePageProcessor implements PageProcessor {
|
public class ZipCodePageProcessor implements PageProcessor {
|
||||||
|
|
||||||
private Site site = Site.me().setCharset("gb2312")
|
private Site site = Site.me().setCharset("gb2312")
|
||||||
.setSleepTime(100).addStartUrl("http://www.ip138.com/post/");
|
.setSleepTime(100);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Page page) {
|
public void process(Page page) {
|
||||||
if (page.getUrl().toString().equals("http://www.ip138.com/post/")) {
|
if (page.getUrl().toString().equals("http://www.ip138.com/post/")) {
|
||||||
processCountry(page);
|
processCountry(page);
|
||||||
} else if (page.getUrl().regex("http://www\\.ip138\\.com/post/\\w+[/]?$").toString() != null) {
|
} else if (page.getUrl().regex("http://www\\.ip138\\.com/\\d{6}[/]?$").toString() != null) {
|
||||||
processProvince(page);
|
|
||||||
} else {
|
|
||||||
processDistrict(page);
|
processDistrict(page);
|
||||||
|
} else {
|
||||||
|
processProvince(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -45,28 +46,26 @@ public class ZipCodePageProcessor implements PageProcessor {
|
||||||
|
|
||||||
private void processProvince(Page page) {
|
private void processProvince(Page page) {
|
||||||
//这里仅靠xpath没法精准定位,所以使用正则作为筛选,不符合正则的会被过滤掉
|
//这里仅靠xpath没法精准定位,所以使用正则作为筛选,不符合正则的会被过滤掉
|
||||||
List<String> districts = page.getHtml().xpath("//body/table/tbody/tr/td").regex(".*http://www\\.ip138\\.com/post/\\w+/\\w+.*").all();
|
List<String> districts = page.getHtml().xpath("//body/table/tbody/tr[@bgcolor=\"#ffffff\"]").all();
|
||||||
|
Pattern pattern = Pattern.compile("<td>([^<>]+)</td>.*?href=\"(.*?)\"",Pattern.DOTALL);
|
||||||
for (String district : districts) {
|
for (String district : districts) {
|
||||||
String link = xpath("//@href").select(district);
|
Matcher matcher = pattern.matcher(district);
|
||||||
String title = xpath("/text()").select(district);
|
while (matcher.find()) {
|
||||||
Request request = new Request(link).setPriority(1).putExtra("province", page.getRequest().getExtra("province")).putExtra("district", title);
|
String title = matcher.group(1);
|
||||||
page.addTargetRequest(request);
|
String link = matcher.group(2);
|
||||||
|
Request request = new Request(link).setPriority(1).putExtra("province", page.getRequest().getExtra("province")).putExtra("district", title);
|
||||||
|
page.addTargetRequest(request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processDistrict(Page page) {
|
private void processDistrict(Page page) {
|
||||||
String province = page.getRequest().getExtra("province").toString();
|
String province = page.getRequest().getExtra("province").toString();
|
||||||
String district = page.getRequest().getExtra("district").toString();
|
String district = page.getRequest().getExtra("district").toString();
|
||||||
List<String> counties = page.getHtml().xpath("//body/table/tbody/tr").regex(".*<td>\\d+</td>.*").all();
|
String zipCode = page.getHtml().regex("<h2>邮编:(\\d+)</h2>").toString();
|
||||||
String regex = "<td[^<>]*>([^<>]+)</td><td[^<>]*>([^<>]+)</td><td[^<>]*>([^<>]+)</td><td[^<>]*>([^<>]+)</td>";
|
page.putField("result", StringUtils.join(new String[]{province, district,
|
||||||
for (String county : counties) {
|
zipCode}, "\t"));
|
||||||
String county0 = regex(regex, 1).select(county);
|
List<String> links = page.getHtml().links().regex("http://www\\.ip138\\.com/\\d{6}[/]?$").all();
|
||||||
String county1 = regex(regex, 2).select(county);
|
|
||||||
String zipCode = regex(regex, 3).select(county);
|
|
||||||
page.putField("result", StringUtils.join(new String[]{province, district,
|
|
||||||
county0, county1, zipCode}, "\t"));
|
|
||||||
}
|
|
||||||
List<String> links = page.getHtml().links().regex("http://www\\.ip138\\.com/post/\\w+/\\w+").all();
|
|
||||||
for (String link : links) {
|
for (String link : links) {
|
||||||
page.addTargetRequest(new Request(link).setPriority(2).putExtra("province", province).putExtra("district", district));
|
page.addTargetRequest(new Request(link).setPriority(2).putExtra("province", province).putExtra("district", district));
|
||||||
}
|
}
|
||||||
|
@ -79,11 +78,8 @@ public class ZipCodePageProcessor implements PageProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Spider.create(new ZipCodePageProcessor()).scheduler(new PriorityScheduler()).run();
|
Spider spider = Spider.create(new ZipCodePageProcessor()).scheduler(new PriorityScheduler()).addUrl("http://www.ip138.com/post/");
|
||||||
|
|
||||||
PriorityScheduler scheduler = new PriorityScheduler();
|
|
||||||
Spider spider = Spider.create(new ZipCodePageProcessor()).scheduler(scheduler);
|
|
||||||
scheduler.push(new Request("http://www.baidu.com/s?wd=webmagic&f=12&rsp=0&oq=webmagix&tn=baiduhome_pg&ie=utf-8"),spider);
|
|
||||||
spider.run();
|
spider.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue