Парсинг большого количества страниц - C#
Формулировка задачи:
Доброго времени суток, товарищи!
Прошу у Вас помощи, самому боюсь не справиться - знаний маловато.
Пишу парсер списка ссылок, и примерно после 80 страниц программа выдает ошибку при выполнении "string html = wc.DownloadString(url)".
Если кто-то знает как подправить программу, чтобы это было более грамотно и эффективно, а главное чтобы ошибок не было, помогите пожалуйста...
using System; using System.IO; using System.Net; using System.Text.RegularExpressions; using System.Threading; namespace Парсер_КиноПоиска { class Parser { static void Main(string[] args) { WebClient wc = new WebClient(); wc.Headers.Add("Accept: text/html, application/xhtml+xml, */*"); wc.Headers.Add("User-Agent: Other"); int page = 1; bool key; string[] id=new string[50000]; int i = 1 ; do { string url = "http://www.kinopoisk.ru/top/navigator/m_act%5Bnum_vote%5D/10/m_act%5Brating%5D/1:/order/num_vote/page/"+page+"/#results"; string html = wc.DownloadString(url); File.WriteAllText(@"D:\file1", html); string mid = @"filmId:(.*?),"; key = false; foreach (Match m in Regex.Matches(html, mid)) { Console.WriteLine(i+") "+m.Groups[1].Value); id[i] = m.Groups[1].Value; File.AppendAllText(@"D:\file2", m.Groups[1].Value + "\n"); i++; key = true; } page++; Thread.Sleep(3000); } while (key); Console.ReadKey(); } } }
Решение задачи: «Парсинг большого количества страниц»
textual
Листинг программы
private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; for (var i = 0; i < 100; i++) { var webRequest3 = WebRequest.Create("http://www.kinopoisk.ru/top/navigator/m_act%5Bnum_vote%5D/10/m_act%5Brating%5D/1:/order/num_vote/page/" + i + "/#results"); webRequest3.BeginGetResponse(ProcessWebRequest, webRequest3); } } private int count = 0; void ProcessWebRequest(IAsyncResult ar) { var webRequest = (WebRequest)ar.AsyncState; var webResponse = webRequest.EndGetResponse(ar); using (Stream stream = webResponse.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.Default); String responseString = reader.ReadToEnd(); string template = "<div class="name"><a href="/level/.*/film/(.*?)/">(.*?)</a>"; foreach (Match m in Regex.Matches(responseString, template)) { richTextBox1.AppendText(String.Format("id = {0}, Name = {1}{2}", m.Groups[1].Value, m.Groups[2].Value, Environment.NewLine)); } Text = "Страниц спарсено: " + count++; } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д