0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cscの作法 その320

Posted at

概要

cscの作法、調べてみた。
練習問題、やってみた。

練習問題

クローラーを実装せよ。

サンプルコード



using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace App 
{
	class test {
		static void Crawler1(string url) {
			ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
			Encoding enc = Encoding.GetEncoding("UTF-8");
			WebRequest req = WebRequest.Create(url);
			WebResponse res = req.GetResponse();
			Stream st = res.GetResponseStream();
			StreamReader sr = new StreamReader(st, enc);
			string txt = sr.ReadToEnd();
			sr.Close();
			st.Close();
			Regex re0 = new Regex("title(.*?)/title");
			Match m = re0.Match(txt);
			if (m.Success) 
			{
				Console.WriteLine(m.Groups[1].Value);
			}
		}
		static void Crawler0(string url) {
			ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
			Encoding enc = Encoding.GetEncoding("UTF-8");
			WebRequest req = WebRequest.Create(url);
			WebResponse res = req.GetResponse();
			Stream st = res.GetResponseStream();
			StreamReader sr = new StreamReader(st, enc);
			string txt = sr.ReadToEnd();
			sr.Close();
			st.Close();
			Regex re0 = new Regex("a href=\"https://qiita.com(/.+?)\" ");
			MatchCollection mc = re0.Matches(txt);
			//foreach (Match m in mc) 
			{
				Console.WriteLine(mc[1].Groups[1].Value);
				Crawler1("https://qiita.com" + mc[1].Groups[1].Value);
			}
		}
		static void Main() {
			Crawler0("https://qiita.com/");
		}
	}
}




以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?