最近部分頁面數(shù)據(jù)被爬蟲瘋狂的使用疏魏,主要就是采用動(dòng)態(tài)代理IP爬取數(shù)據(jù)停做,主要是不控制頻率晤愧,這個(gè)最惡心。因?yàn)閷?duì)方是采用動(dòng)態(tài)代理的方式蛉腌,所以沒什么特別好的防止方式官份。
具體防止抓取數(shù)據(jù)方案大全,下篇博客我會(huì)做一些講解烙丛。本篇也是防爬蟲的一個(gè)方案舅巷。就是部分核心文字采用圖片輸出。加大數(shù)據(jù)抓取方的成本河咽。
圖片輸出需求
上圖紅色圈起來的數(shù)據(jù)為圖片輸出了備案號(hào)钠右,就是要達(dá)到這個(gè)效果,如果數(shù)據(jù)抓取方要繼續(xù)使用忘蟹,必須做圖片解析飒房,成本和難度都加到了。也就是我們達(dá)到的效果了媚值。
Java代碼實(shí)現(xiàn)
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Paths;
public class ImageDemo {
? ? public static void main(String[] args) throws Exception {
? ? ? ? System.out.println(System.currentTimeMillis());
? ? ? ? //輸出目錄
? ? ? ? String rootPath = "/Users/sojson/Downloads/";
? ? ? ? //這里文字的size狠毯,建議設(shè)置大一點(diǎn),其實(shí)就是像素會(huì)高一點(diǎn)褥芒,然后縮放后嚼松,效果會(huì)好點(diǎn),最好是你實(shí)際輸出的倍數(shù),然后縮放的時(shí)候献酗,直接按倍數(shù)縮放即可寝受。
? ? ? ? Font font = new Font("微軟雅黑", Font.PLAIN, 130);
? ? ? ? createImage("https://www.sojson.com", font, Paths.get(rootPath, "sojson-image.png").toFile());
? ? }
? ? private static int[] getWidthAndHeight(String text, Font font) {
? ? ? ? Rectangle2D r = font.getStringBounds(text, new FontRenderContext(
? ? ? ? ? ? ? ? AffineTransform.getScaleInstance(1, 1), false, false));
? ? ? ? int unitHeight = (int) Math.floor(r.getHeight());//
? ? ? ? // 獲取整個(gè)str用了font樣式的寬度這里用四舍五入后+1保證寬度絕對(duì)能容納這個(gè)字符串作為圖片的寬度
? ? ? ? int width = (int) Math.round(r.getWidth()) + 1;
? ? ? ? // 把單個(gè)字符的高度+3保證高度絕對(duì)能容納字符串作為圖片的高度
? ? ? ? int height = unitHeight + 3;
? ? ? ? return new int[]{width, height};
? ? }
? ? // 根據(jù)str,font的樣式以及輸出文件目錄
? ? public static void createImage(String text, Font font, File outFile)
? ? ? ? ? ? throws Exception {
? ? ? ? // 獲取font的樣式應(yīng)用在輸出內(nèi)容上整個(gè)的寬高
? ? ? ? int[] arr = getWidthAndHeight(text, font);
? ? ? ? int width = arr[0];
? ? ? ? int height = arr[1];
? ? ? ? // 創(chuàng)建圖片
? ? ? ? BufferedImage image = new BufferedImage(width, height,
? ? ? ? ? ? ? ? BufferedImage.TYPE_INT_BGR);//創(chuàng)建圖片畫布
? ? ? ? //透明背景? the begin
? ? ? ? Graphics2D g = image.createGraphics();
? ? ? ? image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
? ? ? ? g=image.createGraphics();
? ? ? ? //透明背景? the end
? ? ? ? /**
? ? ? ? ? ? 如果你需要白色背景或者其他顏色背景可以直接這么設(shè)置,其實(shí)就是滿屏輸出的顏色
? ? ? ? ? ? 我這里上面設(shè)置了透明顏色罕偎,這里就不用了
? ? ? ? */
? ? ? ? //g.setColor(Color.WHITE);
? ? ? ? //畫出矩形區(qū)域羡蛾,以便于在矩形區(qū)域內(nèi)寫入文字
? ? ? ? g.fillRect(0, 0, width, height);
? ? ? ? /**
? ? ? ? * 文字顏色,這里支持RGB锨亏。new Color("red", "green", "blue", "alpha");
? ? ? ? * alpha 我沒用好痴怨,有用好的同學(xué)可以在下面留言,我開始想用這個(gè)直接輸出透明背景色器予,
? ? ? ? * 然后輸出文字浪藻,達(dá)到透明背景效果,最后選擇了乾翔,createCompatibleImage Transparency.TRANSLUCENT來創(chuàng)建爱葵。
? ? ? ? * android 用戶有直接的背景色設(shè)置,Color.TRANSPARENT 可以看下源碼參數(shù)反浓。對(duì)alpha的設(shè)置
? ? ? ? */
? ? ? ? g.setColor(Color.gray);
? ? ? ? // 設(shè)置畫筆字體
? ? ? ? g.setFont(font);
? ? ? ? // 畫出一行字符串
? ? ? ? g.drawString(text, 0, font.getSize());
? ? ? ? // 畫出第二行字符串萌丈,注意y軸坐標(biāo)需要變動(dòng)
? ? ? ? g.drawString(text, 0, 2 * font.getSize());
? ? ? ? //執(zhí)行處理
? ? ? ? g.dispose();
? ? ? ? // 輸出png圖片,formatName 對(duì)應(yīng)圖片的格式
? ? ? ? ImageIO.write(image, "png", outFile);
? ? }
}
輸出圖片效果:
當(dāng)然我這里是做了放縮雷则,要不然效果沒那么好辆雾。
注意點(diǎn):
其實(shí)代碼里注釋說的已經(jīng)比較清楚了。主要設(shè)置透明色這里月劈。
//透明背景? the begin
Graphics2D g = image.createGraphics();
image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g=image.createGraphics();
//透明背景? the end
Android 參考的顏色值
android.graphics.Color 包含顏色值
Color.BLACK ? ? ? ? ? 黑色
Color.BLUE ? ? ? ? ? ?藍(lán)色
Color.CYAN ? ? ? ? ? ?青綠色
Color.DKGRAY ? ? ? ? ?灰黑色
Color.GRAY ? ? ? ? ? ?灰色
Color.GREEN ? ? ? ? ? 綠色
Color.LTGRAY ? ? ? ? ?淺灰色
Color.MAGENTA ? ? ? ? 紅紫色
Color.RED ? ? ? ? ? ? 紅色
Color.TRANSPARENT ? ? 透明
Color.WHITE ? ? ? ? ? 白色
Color.YELLOW ? ? ? ? ?黃色
版權(quán)所屬:SO JSON在線解析