用c#編寫爬蟲在marinetraffic下載船僅僅圖片

近期在做船僅僅識別方面的事情,須要大量的正樣本來訓練adaboost分類器。

于是到marinetraffic這個站點上下載船僅僅圖片。寫個爬蟲來自己主動下載顯然非常方便。

站點特點

在介紹爬蟲之前首先了解一下marinetraffic這個站點的一些特點:
1. 會定期檢測爬蟲行為。假設覺得有爬蟲大量下載圖片。

會把該連接增加黑名單,后幾天都沒辦法下載。
2. 船僅僅圖片資源差異大。有的船僅僅有1000多張圖,有的船僅僅沒有一張圖,我們須要的是非常多船僅僅的非常多張圖。所以須要對下載的船僅僅按優先級排序。
3. 用來訓練分類器的正樣本要求檢測對象的分辨率一樣。而marinetraffic站點下載的圖片能夠設置下在的圖片的寬度,站點依據長寬比,生成對應的高度。所以。不同圖片高度不一樣。須要自己后期處理。

船僅僅圖片

解決方式

  1. 針對爬蟲檢測。設置一個隨機等待時間,10s左右。能夠繞過站點爬蟲行為檢測。
  2. 對船僅僅依照圖片熟練排序,先下載圖片數量多的,而且每一個船僅僅不用下載太多。保證圖片的差異性。比如
  3. 在下載的時候使用統一的寬度。

    后期處理從圖片中摳出分辨率一樣的船僅僅

爬蟲源代碼

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;namespace 船僅僅圖像爬蟲
{class Program{static void download_all_shipid(List<string> shipid_list){try{WebClient MyWebClient = new WebClient();MyWebClient.Headers["User-Agent"] = "blah";MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用于向Internet資源的請求進行身份驗證的網絡憑據;//Console.WriteLine("here1");//http://www.marinetraffic.com/en/photos/of/ships/shipid:281519///http://www.marinetraffic.com/en/ais/index/ships/all//http://www.marinetraffic.com/ais/index/ships/all/page:2/sort:COUNT_PHOTOS/direction:desc;for (int pageNum = 1; pageNum < 100; pageNum++){Console.WriteLine("開始分析第" + pageNum + "張網頁");MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用于向Internet資源的請求進行身份驗證的網絡憑據;MyWebClient.Headers["User-Agent"] = "blah";try{//Console.WriteLine("here0");Byte[] pageData = MyWebClient.DownloadData(@"http://www.marinetraffic.com/en/ais/index/ships/all/page:" + pageNum + "/sort:COUNT_PHOTOS/direction:desc/per_page:50"); //從指定站點下載數據//pageHtml = Encoding.Default.GetString(pageData);  //假設獲取站點頁面採用的是GB2312,則使用這句;            string pageHtml = Encoding.UTF8.GetString(pageData); //假設獲取站點頁面採用的是UTF-8。則使用這句;//Console.WriteLine(pageHtml);//在控制臺輸入獲取的內容;//Console.WriteLine("here1");int urlindex = -1;string org_label = "shipid:";urlindex = pageHtml.IndexOf(org_label, urlindex + 1);while (urlindex != -1){int endOfUrl = pageHtml.IndexOf("/", urlindex + org_label.Length);//Console.WriteLine("here2");string shipid = pageHtml.Substring(urlindex + org_label.Length, endOfUrl - urlindex - org_label.Length);if (!shipid_list.Contains(shipid)){Console.WriteLine("新增id:" + shipid);shipid_list.Add(shipid);}//Console.WriteLine("已有id:" + shipid);urlindex = pageHtml.IndexOf(org_label, urlindex + 1);}///保存網頁//using (StreamWriter sw = new StreamWriter("ouput.html"))//將獲取的內容寫入文本//{//    sw.Write(pageHtml);//}Console.WriteLine("完畢第" + pageNum + "頁分析");}catch (WebException webEx){Console.WriteLine(webEx.Message.ToString());}//以下是一個隨機數的方法保證10秒后再下載。以繞過違規檢測。Console.Write("繞開站點爬蟲行為檢測中......");Random rd = new Random();int time_sleep = rd.Next() % 10 + 10;Thread.Sleep(time_sleep * 1000);Console.WriteLine();}Console.WriteLine("分析結束");//以下把list內容保存進文件,使用序列化的方法;string file = @"C:\Users\dragonfive\Desktop\爬蟲獲得船僅僅圖片\第三批\0_100page_shipid.txt";using (FileStream fsWriter = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write)){//以下對stu進行序列化。BinaryFormatter bf = new BinaryFormatter();bf.Serialize(fsWriter, shipid_list);}}catch (WebException webEx){Console.WriteLine(webEx.Message.ToString());}}/// <summary>/// 依據得到的ship_id獲得該ship_id的全部圖片;/// </summary>/// <param name="ship_id"></param>static void download_jpg(string ship_id){try{Console.WriteLine("開始下載shipid為:"+ship_id+"的圖片");WebClient MyWebClient = new WebClient();MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用于向Internet資源的請求進行身份驗證的網絡憑據MyWebClient.Headers["User-Agent"] = "blah";//http://www.marinetraffic.com/en/photos/of/ships/shipid:281519///http://www.marinetraffic.com/en/photos/of/ships/shipid:371668/per_page:1000/page:1Byte[] pageData = MyWebClient.DownloadData(@"http://www.marinetraffic.com/en/photos/of/ships/shipid:" + ship_id + @"/per_page:100/page:1"); //從指定站點下載數據//string pageHtml = Encoding.Default.GetString(pageData);  //假設獲取站點頁面採用的是GB2312。則使用這句            string pageHtml = Encoding.UTF8.GetString(pageData); //假設獲取站點頁面採用的是UTF-8,則使用這句//Console.WriteLine(pageHtml);//在控制臺輸入獲取的內容Console.WriteLine("元網頁已下載");//using (StreamWriter sw = new StreamWriter("ouput.html"))//將獲取的內容寫入文本//{//    sw.Write(pageHtml);//}int urlindex = -1;string org_label = "data-original='";urlindex = pageHtml.IndexOf(org_label, urlindex + 1);int i = 0;//Directory.CreateDirectory(@"./" );while (urlindex != -1){int endOfUrl = pageHtml.IndexOf("'", urlindex + org_label.Length);string url = pageHtml.Substring(urlindex + org_label.Length, endOfUrl - urlindex - org_label.Length);////以下是unicode編碼轉換為string的方式;//MatchCollection mc = Regex.Matches(strName, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);//byte[] bts = new byte[2];//foreach (Match m in mc)//{//    bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);//    bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);//    musicName += Encoding.Unicode.GetString(bts);//}//Console.WriteLine("接下來下載的是:" + musicName);//以下是一個隨機數的方法保證10秒后再下載。以繞過違規檢測。Console.Write("繞過站點爬蟲行為檢測中......");Random rd = new Random();int time_sleep = rd.Next() % 10 + 10;Thread.Sleep(time_sleep * 1000);Console.WriteLine();try{//這是下載的命令;Console.WriteLine(url);MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用于向Internet資源的請求進行身份驗證的網絡憑據MyWebClient.Headers["User-Agent"] = "blah";Byte[] jpgdata = MyWebClient.DownloadData(url); //從指定網頁下載數據;//把下載的內容保存在一個地方;using (FileStream fs = new FileStream(@"C:\Users\dragonfive\Desktop\爬蟲獲得船僅僅圖片\第三批\" + ship_id + "_" + i + ".jpg", FileMode.OpenOrCreate, FileAccess.Write)){fs.Write(jpgdata, 0, jpgdata.Length);}}catch (WebException webEx){Console.WriteLine("被捕獲了嗎?");Console.WriteLine(webEx.Message.ToString());}Console.WriteLine("成功下載第" + (i ++) + "張圖片");urlindex = pageHtml.IndexOf(org_label, urlindex + 1);}///保存網頁//using (StreamWriter sw = new StreamWriter("ouput.html"))//將獲取的內容寫入文本//{//    sw.Write(pageHtml);//}Console.WriteLine("*****************************************");Console.WriteLine("下載"+i+"張ship_id為"+ship_id+"的圖片");Console.WriteLine("*****************************************");//Console.ReadLine(); //讓控制臺暫停,否則一閃而過了 }catch (WebException webEx){Console.WriteLine(webEx.Message.ToString());}}static void Main(string[] args){List<string> shipid_list = new List<string>();//shipid_list.Add("371681");//臨時高速產生圖片用這個;download_all_shipid(shipid_list);//string file = @"C:\Users\dragonfive\Desktop\爬蟲獲得船僅僅圖片\第三批\0_100page_shipid.txt";//using (FileStream fsReader = new FileStream(file, FileMode.Open, FileAccess.Read))//{//    //以下進行反序列話;//    BinaryFormatter bf = new BinaryFormatter();//    shipid_list = (List<string>)bf.Deserialize(fsReader);//    Console.WriteLine("成功加載" + shipid_list.Count + "個shipid");//}////371652 371668  371681 1252401 //shipid_list.Remove("371652");//shipid_list.Remove("371668");//shipid_list.Remove("371681");//shipid_list.Remove("1252401");////132264//shipid_list.Remove("371077");//shipid_list.Remove("132264");//shipid_list.Remove("224871");//shipid_list.Remove("279923");//shipid_list.Remove("369163");//shipid_list.Remove("266342");//shipid_list.Remove("371216");//shipid_list.Remove("368174");//shipid_list.Remove("369163");foreach (var ship_id in shipid_list){download_jpg(ship_id);}Console.ReadLine(); //讓控制臺暫停,否則一閃而過了 }}
}

轉載于:https://www.cnblogs.com/yutingliuyl/p/6941828.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/456309.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456309.shtml
英文地址,請注明出處:http://en.pswp.cn/news/456309.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

發送手機驗證碼通過調用第三方網易云信API(flask項目)

一、 獲取驗證碼&#xff1a; 1. 輸入手機號碼 2. 通過ajax發送請求 3. 后端&#xff1a; 獲取手機號碼 使用requests向第三方的服務端&#xff08;網易云信&#xff09;發送請求 官方文檔 https://dev.yunxin.163.com/docs/product/%E7%9F%AD%E4%BF%A1/%E7%9F…

異常檢測算法之LOF

前言&#xff1a; LOF&#xff1a;Local outlier factor&#xff0c;即局部異常因子。LOF主要是通過比較每個點p和其鄰域點的密度來判斷該點是否為異常點&#xff0c;如果點p的密度越低&#xff0c;越可能被認定是異常點。至于密度&#xff0c;是通過點之間的距離來計算的&…

Android屬性動畫進階用法

2019獨角獸企業重金招聘Python工程師標準>>> 在上周二文章中介紹補間動畫缺點的時候有提到過&#xff0c;補間動畫是只能對View對象進行動畫操作的。而屬性動畫就不再受這個限制&#xff0c;它可以對任意對象進行動畫操作。那么大家應該還記得之前我舉的一個例子&am…

5.3linux下C語言socket網絡編程簡例

原創文章&#xff0c;轉載請注明轉載字樣和出處&#xff0c;謝謝&#xff01; 這里給出在Linux下的簡單socket網絡編程的實例&#xff0c;使用tcp協議進行通信&#xff0c;服務端進行監聽&#xff0c;在收到客戶端的連接后&#xff0c;發送數據給客戶端&#xff1b;客戶端在接受…

parser.add_argument驗證格式

article_bp Blueprint(article, __name__, url_prefix/api) api Api(article_bp) parser reqparse.RequestParser() parser.add_argument(name, typestr, help必須填寫名稱, requiredTrue) channel_fields { id: fields.Integer, cname: fields.String } clas…

異常檢測算法之HBOS

前言 HBOS&#xff08;Histogram-based Outlier Score&#xff09;核心思想&#xff1a;將樣本按照特征分成多個區間&#xff0c;樣本數少的區間是異常值的概率大。 原理 該方法為每一個樣本進行異常評分&#xff0c;評分越高越可能是異常點。評分模型為&#xff1a; 假設樣…

字典和json 的區別 和轉換

前言&#xff1a;字典和json非常像。接下來比較一下兩者的異同 先看一下字典的寫法&#xff1a; a {a:1,b:2,c:3} 再看一下json的寫法&#xff1a; {"studentInfo":{"id":123456,"stu_name":"Dorra"} } 從形式上看&#xff0c;都是…

Struts2的工作原理及工作流程

眾所周知&#xff0c;Struts2是個非常優秀的開源框架&#xff0c;我們能用Struts2框架進行開發&#xff0c;同時能 快速搭建好一個Struts2框架&#xff0c;但我們是否能把Struts2框架的工作原理用語言表達清楚&#xff0c;你表達的原理不需要說出底層是怎么實現的&#xff0c;我…

正則表達式采坑

[a-zA-Z]匹配大小寫字符 \w 匹配字母、數字、下劃線 {5,7} 表示前面的字符&#xff08;即&#xff1a;\w&#xff09;必須至少出現 5 次最多出現 7 次. 合起來就是 >6 少于8個的字符 [a-zA-Z]\w{6,12} --------------》》 就是要輸入七位數到十三位&#x…

easyui動態顯示和隱藏表頭

為什么80%的碼農都做不了架構師&#xff1f;>>> var _bt{date:日期,subtime:填寫時間,xz:小組,uname:操作人,qdbh:渠道編號,mt:媒體,zh:賬戶,sjd:時間段,tfwz:投放位置,tfh:投放號,td:團隊,sjje:實際金額,jxs:進線數,cb:成本,yxzyjx:有效資源進線,yxzyl:有效資源率…

物聯網

如果要說未來什么技術正在或將徹底改變人類生活、工作和娛樂的方式&#xff0c;那必須是物聯網。小到各種可穿戴產品&#xff0c;大到汽車、工廠和樓宇&#xff0c;物聯網能使一切設備互聯并具備智慧。物聯網也正改變著產業的格局&#xff0c;索尼、夏普、東芝等日本傳統電子設…

理解:復雜度是O(log^n) 就是二分法

冒昧問一下&#xff0c;為什么二分法查找的復雜度是O(log^n)&#xff1f;這是怎么計算的&#xff1f; 你要從1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5&#xff0c;6&#xff0c;7&#xff0c;8里面找到3&#xff0c;分成幾步&#xff1f; 第一步&#xff0c;…

淺談管理數據平臺的一些想法

前言&#xff1a; 對于任何使用大數據技術的公司來說&#xff0c;大數據平臺特別是Hive來說&#xff0c;維護其高效快速的運行&#xff0c;對整個公司的運作來說至關重要。比如說&#xff1a;某個調度任務失敗了造成業務部門的某些報表無法正常產出&#xff1b;hive平臺最近速…

MongoDB誤刪表恢復

一、場景描述公司某工程師執行db.giveget_card.drop()&#xff0c;誤將線上表刪除。幸好每天都有做備份&#xff0c;這個時候就體現了備份的重要性了&#xff0c;哈哈哈。。。二、模擬故障過程備份數據大小&#xff1a;rs_test01:PRIMARY> use ycsb switched to db ycsb rs_…

linux下kill某個應用

linux命令行與桌面切換快捷鍵CtrAltF1&#xff0c;CtrAltF7 ps -e | grep abc sudo kill xyz 轉載于:https://www.cnblogs.com/cj2014/p/6512354.html

flask中數據庫的基本操作-增刪改查【備忘】

1.增加數據&#xff08;就相當于增加一個實例對象&#xff09; user1 User(namelong,email1006550026qq.com,password123456,role_id1) db.session.add(user1) db.session.commit() 2.修改數據 修改用戶表里面的name為long的姓名為&#xff1a;fang 首先查詢到名為…

兩個文件比較之comm命令

comm命令可用于兩個文件之間的比較。它有很多不錯的選項可用來調整輸出&#xff0c;以便我們執行交集、求差&#xff08;difference&#xff09;以及差集操作。? 交集&#xff1a;打印出兩個文件所共有的行。? 求差&#xff1a;打印出指定文件所包含的且互不相同的那些行。?…

【轉】error while loading shared libraries: xxx.so.x 錯誤的原因和解決辦法

原博客地址&#xff1a;http://www.cnblogs.com/Anker/p/3209876.html#undefined error while loading shared libraries: xxx.so.x" 錯誤的原因和解決辦法 今天在執行一個protobuf程序時&#xff0c;提示error while loading shared libraries: libprotobuf.so.8: cannot…

Flask學習記錄之Flask-SQLAlchemy

Flask-SQLAlchemy庫讓flask更方便的使用SQLALchemy,是一個強大的關系形數據庫框架,既可以使用orm方式操作數據庫,也可以使用原始的SQL命令. Flask-Migrate 是一個數據遷移框架,需要通過Flask-script庫來操作. 一.配置Flask-SQLAlchemy 程序使用的數據庫地址需要配置在SQLALCH…

Postico —— OS X 上的免費 PostgreSQL 客戶端

Postico 是 OS X 下的一個 PostgreSQL 客戶端管理工具。要求 OS X 10.8 或者更新版本。 文章轉載自 開源中國社區 [http://www.oschina.net]