国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 建站知識(shí) > 爬蟲實(shí)現(xiàn):根據(jù)IP地址反查域名

爬蟲實(shí)現(xiàn):根據(jù)IP地址反查域名

時(shí)間:2023-02-12 03:48:01 | 來源:建站知識(shí)

時(shí)間:2023-02-12 03:48:01 來源:建站知識(shí)

域名解析與IP地址

域名解析是把域名指向網(wǎng)站空間IP,讓人們通過注冊(cè)的域名可以方便地訪問到網(wǎng)站的一種服務(wù);IP地址是網(wǎng)絡(luò)上標(biāo)識(shí)站點(diǎn)的數(shù)字地址,為了方便記憶,采用域名來代替IP地址標(biāo)識(shí)站點(diǎn)地址。域名解析就是域名到IP地址的轉(zhuǎn)換過程,該過程由DNS服務(wù)器完成(來自百度百科)

先來了解兩個(gè)知識(shí)點(diǎn)

1、一個(gè)域名同一時(shí)刻只能對(duì)應(yīng)一個(gè)IP地址

2、一個(gè)IP地址可以解析綁定多個(gè)域名,沒有限制

基于以上知識(shí)點(diǎn),假如我們已知一個(gè)IP地址,我們?cè)趺床拍塬@取解析到該IP地址的所有域名信息呢?一種方式是國(guó)家工信部能開放查詢接口以供查詢(不知道會(huì)不會(huì)開放?);另外一種方式就是接下來我要分享的——爬蟲實(shí)現(xiàn):根據(jù)IP地址反查域名。

實(shí)現(xiàn)原理

實(shí)現(xiàn)原理其實(shí)很簡(jiǎn)單,現(xiàn)在已有網(wǎng)站提供了根據(jù)IP地址查詢域名的功能,但是需要人為登錄網(wǎng)站輸入IP地址查詢,我想要實(shí)現(xiàn)程序自動(dòng)化查詢,所以就想到了爬蟲的方式,簡(jiǎn)單來說,就是模擬人的查詢行為,將查詢結(jié)果解析成我想要的域名列表。

site.ip138.com為例,打開F12,輸入一個(gè)IP查詢,觀察控制臺(tái)請(qǐng)求,看到下圖中信息

請(qǐng)求地址為:http://site.ip138.com/119.75.217.109/

請(qǐng)求方式為:GET







然后,分析Response,可以看到,在頁(yè)面上看到的綁定域名信息就是下圖紅框中<span>的內(nèi)容,所以只要能將Response的內(nèi)容解析出來,獲取到<span>的內(nèi)容就可以得到想要的域名列表。







上述Response是HTML頁(yè)面,使用jsoup來解析HTML簡(jiǎn)直完美。

jsoup是什么?

jsoup 是一款Java 的HTML解析器,可直接解析某個(gè)URL地址、HTML文本內(nèi)容。它提供了一套非常省力的API,可通過DOM,CSS以及類似于jQuery的操作方法來取出和操作數(shù)據(jù)。
//解析成Document對(duì)象Document document = Jsoup.parse(result);if (document == null) { logger.error("Jsoup parse get document null!");}//根據(jù)ID屬性“l(fā)ist”獲取元素Element對(duì)象(有沒有感覺很像jQuery?)Element listEle = document.getElementById("list");//根據(jù)class屬性和屬性值篩選元素Element集合,并通過eachText()遍歷元素內(nèi)容return listEle.getElementsByAttributeValue("target", "_blank").eachText();result的內(nèi)容通過HttpClient模擬HTTP請(qǐng)求

HttpGet httpGet = new HttpGet(url);httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");httpGet.setHeader("Accept-Encoding", "gzip, deflate");httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");httpGet.setHeader("Cache-Control", "max-age=0");httpGet.setHeader("Connection", "keep-alive");httpGet.setHeader("Cookie", "Hm_lvt_d39191a0b09bb1eb023933edaa468cd5=1553090128; BAIDU_SSP_lcr=https://www.baidu.com/link?url=FS0ccst469D77DpdXpcGyJhf7OSTLTyk6VcMEHxT_9_&wd=&eqid=fa0e26f70002e7dd000000065c924649; pgv_pvi=6200530944; pgv_si=s4712839168; Hm_lpvt_d39191a0b09bb1eb023933edaa468cd5=1553093270");httpGet.setHeader("DNT", "1");httpGet.setHeader("Host", host);httpGet.setHeader("Upgrade-Insecure-Requests", "1");httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");String result = HttpUtils.doGet(httpGet);HTTP請(qǐng)求工具類

public class HttpUtils { private static Logger logger = LoggerFactory.getLogger(HttpUtils.class); public static String doGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(5000).setConnectionRequestTimeout(10000) .setSocketTimeout(5000).build(); httpGet.setConfig(requestConfig); HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200 || httpResponse.getStatusLine().getStatusCode() == 302) { HttpEntity entity = httpResponse.getEntity(); return EntityUtils.toString(entity, "utf-8"); } else { logger.error("Request StatusCode={}", httpResponse.getStatusLine().getStatusCode()); } } catch (Exception e) { logger.error("Request Exception={}:", e); } finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { logger.error("關(guān)閉httpClient失敗", e); } } } return null; }}新增Controller

@RestControllerpublic class DomainSpiderController { private static Logger logger = LoggerFactory.getLogger(DomainSpiderController.class); @Autowired private DomainSpiderService domainSpiderService; /** * @param ip 119.75.217.109 * @return */ @RequestMapping("/spider/{ip}") @ResponseBody public List<String> domainSpider(@PathVariable("ip") String ip) { long startTime = System.currentTimeMillis(); List<String> domains = domainSpiderService.domainSpiderOfIp138(ip); if(domains == null || domains.size() == 0) { domains = domainSpiderService.domainSpiderOfAizan(ip); } long endTime = System.currentTimeMillis(); logger.info("完成爬蟲任務(wù)總耗時(shí):{}s", (endTime - startTime) / 1000); return domains; }}啟動(dòng)Spring Boot應(yīng)用,訪問瀏覽器:http://localhost:8080/spider/119.75.217.109 獲得返回結(jié)果如下:




怎么樣?是不是很簡(jiǎn)單?

優(yōu)化改進(jìn):有時(shí)候僅僅通過一個(gè)網(wǎng)站查詢的域名數(shù)據(jù)可能不太準(zhǔn)確,甚至查詢不到數(shù)據(jù),我們也沒法判斷誰(shuí)才是正確的,所以,可以通過爬取多個(gè)網(wǎng)站的結(jié)果結(jié)合起來使用,例如:dns.aizhan.com

提出疑問:這些提供根據(jù)IP反查域名的網(wǎng)站,是怎么實(shí)現(xiàn)的呢?我咨詢過其他人,他們的回答是這些網(wǎng)站收集了很多IP和域名的對(duì)應(yīng)關(guān)系,真實(shí)情況是這樣的嗎?

示例源碼

代碼已上傳至碼云Github上,歡迎下載學(xué)習(xí)

關(guān)鍵詞:地址,實(shí)現(xiàn),根據(jù),爬蟲

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉