下面两个包见我的空间资源 handy.jar,log4j.jar
HttpConnection 通过使用网页上的查询功能,提交 setRequestMethod(HttpConnection.REQUEST_METHOD_GET);
....
import java.util.HashMap;
import com.handy.socket.HttpConnection;
import com.handy.util.FileTool;
public class MobileCity {
public MobileCity() {
super();
}
public static void main(String[] args) {
HttpConnection hc = new HttpConnection();
StringBuffer sb = new StringBuffer();
String fileName="d:\\m.txt";
String ms = FileTool.readFileToString(fileName);
String[] _mss = ms.split("\n");
int i=0;
for (String m:_mss){
m=m.trim();
i++;
//System.out.println(i+"."+m);
//手机号归属地
//String m="18017122529";
String url="http://www.shoujichahao.com/search.jsp?numb="+m;
System.out.println(url);
hc.setRequestMethod(HttpConnection.REQUEST_METHOD_GET);
hc.setUrl(url);
hc.setCharset("utf-8");
hc.setStartLine(69);
hc.setEndLine(91);
String str = hc.getHttpText();
str = str.replace(" ", "").replaceAll("<[^>]+>", "").replace("归属地", "").replace("手机卡","");
//System.out.println(str);
HashMap<String,String> areacode = new HashMap<String,String>();
String[] lines = str.split("\n");
int nu=1;
for (String line:lines){
line = line.trim();
if (!"".equals(line)){
if (nu>2) break;
System.out.println(line);
if (line.indexOf(" ")>0){
String[] ab = line.split(" ");
areacode.put("Province", ab[0]);
areacode.put("City", ab[1]);
}else{
areacode.put("Card", line);
}
nu++;
}
}
String sql = "update sendsms_jian30 set province='"+areacode.get("Province")+"',city='"+areacode.get("City")+"',card='"+areacode.get("Card")+"' where addresses='tel:"+m+"'";
if (areacode.get("Card")!=null) sb.append(sql+"\r\n");
System.out.println(i+":"+sql);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
fileName = "d:\\m_city.sql";
FileTool.writeStringToFile(fileName, sb.toString());
}
}