前段时间项目组要做一个城市交通路况信息的报告,需要对路况数据进行抓取,细分了一下大概有一万多个路口需要抓取,抓取力度是五分钟抓取一次,抓取范围是早晚高峰。
着手干活:
1.url形如http://tm.amap.com/trafficengine/mapabc/traffictile?v=1.0&;t=1&zoom=0&x=108243&y=53281
2.对URL发送请求后,返回的是一张图片,图片上展示的是一个路口的路况信息
3.刚开始享用python对数据进行抓取,于是查了一下python对图片的抓取,可惜效果不太好
4.最后想到了linux下的curl命令,他可以将返回的数据直接保存成文件或者图片等,于是对其进行了测试,发现果然可以
5.开始进行shell脚本编程具体如下(不懂shell脚本的可以网上看一下,很简单的):
#!/bin/bash
filename="/home/test/pic"
if [ -d $filename ]
then
echo "目录存在"
fi
indexs=`expr index "$filename" j`
echo $indexs
echo ${filename:`expr ${indexs} - 1`:7}
datestr=$(date +%Y%m%d%H%M)
filepath="/mnt/pic/$datestr"
if [ -d $filepath ]
then
echo "目录存在"
else
mkdir $filepath
fi
while read line
do
echo $line
#xx=`expr index "$line" x=`
xx=`expr index "$line" x`
yy=`expr index "$line" y`
# echo $xx
# echo $yy
# cha=`expr ${yy} - ${xx}`
# echo $cha
xstr=${line:`expr ${xx} + 1`:6}
ystr=${line:`expr ${yy} + 1`:5}
name="${xstr}_${ystr}_${datestr}.png"
lastname=${filepath}/$name
curl $line >>${lastname}
done <"/home/jinjuan/gaode_tile_urllist_hf_one.txt"
6.然后再用crontab编写定时任务,每五分钟运行一次,大功告成
7.结果查看,发现结果与我的url个数相差较多,查看得知在有限的五分钟内没有将所有的url都爬取到,原来curl也是有一些默认的请求响应时间的,于是我将原始url拆分成多个url文件,测试发现两分钟就跑完了一万条请求,心想这下可以交差了,但仔细一想还没考虑到被封IP的情况,于是查看了curl命令如何使用代理(比较简单),在网上爬取了一些代理,让后对代理进行可用性测试,找到了请求较快的50个代理,大功告成。
以下是用java写的测试代理可行性的代码:
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
public class test {
/** 设置代理
* @param ip
* @param port
* @return
*/
public static boolean createIPAddress(String ip,int port) {
URL url = null;
boolean flag=false;
try {
url = new URL("http://tm.amap.com/trafficengine/mapabc/traffictile?v=1.0&;t=1&zoom=0&x=108251&y=53250");
} catch (MalformedURLException e) {
System.out.println("url invalidate");
}
InetSocketAddress addr = null;
addr = new InetSocketAddress(ip, port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy
InputStream in = null;
try {
URLConnection conn = url.openConnection(proxy);
conn.setConnectTimeout(50);
conn.setReadTimeout(1000);
in = conn.getInputStream();
} catch (Exception e) {
System.out.println("ip " + ip + " is not aviable");//异常IP
}
String s = convertStreamToString(in);
System.out.println(s);
// System.out.println(s);
if(s!=""){
System.out.println(ip + ":"+port+ " is ok");
flag=true;
}
return flag;
}
/** 读取返回的数据
* @param is
* @return
*/
public static String convertStreamToString(InputStream is) {
if (is == null)
return "";
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/** 载入所有代理
* @return
* @throws Exception
*/
public static Map<String,Integer> readAllIP() throws Exception{
BufferedReader ro =new BufferedReader(new FileReader("E:\\proxys.txt"));
String ws =null;
String str =null;
Map<String,Integer> map = new HashMap<String,Integer>();
while((ws=ro.readLine())!=null){
str=ws;
JSONObject par = JSON.parseObject(str);
map.put(par.getString("ip"), par.getIntValue("port"));
}
ro.close();
return map;
}
public static void main(String[] args) throws Exception {//
Map<String, Integer> readAllIP = readAllIP();
System.out.println(readAllIP.size());
boolean createIPAddress = createIPAddress("121.232.146.110", 9000);
System.out.println(createIPAddress);
/* BufferedWriter wo = new BufferedWriter(new FileWriter("E:\\use_proy.txt",true));
for(String str:readAllIP.keySet()){
boolean createIPAddress = createIPAddress(str, readAllIP.get(str));
if(createIPAddress){
System.out.println("可用ip"+str+":"+readAllIP.get(str));
wo.write(str+":"+readAllIP.get(str));
wo.newLine();
wo.flush();
}else{
System.out.println("当前ip"+str+":"+readAllIP.get(str));
}
}
wo.close(); */
}
}
crontab定时任务:
*/5 7-9 * * * /home/test/shes/test.sh /dev/null 2>&1
*/5 17-19 * * * /home/test/shes/test.sh /dev/null 2>&1
*/5 7-9 * * * /home/test/shes/test_two.sh /dev/null 2>&1
*/5 17-19 * * * /home/test/shes/test_two.sh /dev/null 2>&1