namespace av
{
public partial class MainForm : Form
{
private Excel.Application ex = new Microsoft.Office.Interop.Excel.Application();
public static string result;
public static string first;
public static string second;
public static string urlo;
public MainForm()
{
InitializeComponent();
ex.Visible = false;
ex.Workbooks.Open(sourcetext.Text,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
LoadAndClick();
}
private async void LoadAndClick()
{
int nach = Convert.ToInt32(strNtext.Text);
int konech = Convert.ToInt32(strAlltext.Text);
for (int i = nach; i <= konech; i++)
{
first = st1text.Text + i.ToString();
second = st2text.Text + i.ToString();
Excel.Worksheet newsheet = (Excel.Worksheet)ex.Worksheets.get_Item(1);
Excel.Range rngfirst = newsheet.Range[first];
Excel.Range rngsec = newsheet.Range[second];
urlo = "https://yandex.ru/images/search?family=yes&text="+rngfirst.Value+"+"+rngsec.Value;
Debug.WriteLine(urlo);
var tcs = new TaskCompletionSource<int>();
HtmlElementEventHandler docCompleted = null;
result = strestext.Text + i.ToString();
docCompleted = (s, e) =>
{
try
{
webBrowser1.Document.Click -= docCompleted;
string element = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).GetAttribute("href");
Debug.WriteLine(element);
Regex regex = new Regex(@"img_url=(.*?)&pos=(.*?)&", RegexOptions.IgnoreCase);
foreach (Match match in regex.Matches(element))
{
Debug.WriteLine("Match 1: " + match.Groups[1].Value);
string sss = System.Uri.UnescapeDataString(match.Groups[1].Value);
Debug.WriteLine(s);
Excel.Worksheet sheet = (Excel.Worksheet)ex.Worksheets.get_Item(1);
Excel.Range rng3 = sheet.Range[result];
rng3.Value = sss;
int to = Convert.ToInt32(timeouttext.Text);
System.Threading.Thread.Sleep(to);
ex.Save();
}
}
finally
{
// отмечаем, что событие отработало.
tcs.TrySetResult(0);
}
};
webBrowser1.Navigate(urlo);
Debug.WriteLine("Загрежно");
webBrowser1.Document.Click += docCompleted;
await tcs.Task; // ждем, когда событие сработает.
}
ex.Workbooks.Close();
}
}
}