Выборка номеров из текста - C#
Формулировка задачи:
Работаю с POST запросами. В ответе получаю И таких позиций в ответе может быть до 100 (два в пример привел). Как получать непосредственно каждую цену по очереди? "price_min":180.00, "price_min":2688.62 и т.д.?
{"error":"","row_count":2,"table":[{"code_cat":11745212,"price_min":180.00},{"code_cat":114289708,"price_min":2688.62}]}Решение задачи: «Выборка номеров из текста»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Net;
using System.Threading;
using System.Text.RegularExpressions;
public class WebRequestPostExample
{
public static void Main()
{
Excel.Application work = new Excel.Application();
Excel.Workbook Objwb = work.Workbooks.Open(@"C:\Users\admin\Desktop\price.xlsx", 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); //открыть файл
Excel.Worksheet ObjWorkSheet = (Excel.Worksheet)Objwb.Sheets[1];
Excel.Workbook outwb = work.Workbooks.Open(@"C:\Users\admin\Desktop\ZZap\out.xlsx", 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); //открыть файл
Excel.Worksheet outWorkSheet = (Excel.Worksheet)outwb.Sheets[1];
outWorkSheet.Cells[1, 1] = "Название";
outWorkSheet.Cells[1, 2] = "Производитель";
outWorkSheet.Cells[1, 3] = "Артикул";
outWorkSheet.Cells[1, 4] = "Номер по ZZap";
outWorkSheet.Cells[1, 5] = "Наша цена";
outWorkSheet.Cells[1, 6] = "Минимум ZZap";
outWorkSheet.Cells[1, 7] = "Средняя ZZap";
outWorkSheet.Cells[1, 8] = "Максимум ZZap";
int U = 7360;
do
{
string i = ObjWorkSheet.Cells[U, 2].Text.ToString(); //Берем данные из Excel
string j = ObjWorkSheet.Cells[U, 3].Text.ToString();
string b = ObjWorkSheet.Cells[U, 5].Text.ToString();
string p = ObjWorkSheet.Cells[U, 6].Text.ToString();
outWorkSheet.Cells[U - 1, 1] = ObjWorkSheet.Cells[U, 1].Text.ToString();
outWorkSheet.Cells[U - 1, 2] = j;
outWorkSheet.Cells[U - 1, 3] = i;
outWorkSheet.Cells[U - 1, 5] = p;
outWorkSheet.Cells[U - 1, 9] = b;
{
if (b == "0")
goto cycle;
}
{
if (i == "")
goto cycle;
}
{
if (j == "AL-KO")
j = "ALKO";
}
{
if (j == "MEAT & DORIA")
j = "MEATDORIA";
}
{
if (j == "DEPO")
j = "";
}
string text = "search_text=";
string row = "row_count=1";
string prob = ";";
string key = "api_key=EAAAAENi20T4HQCS2eQETQj1/lws9GI/HzBpRnVUwFR7V2dH";
const string SourceFile = "Mytext2.txt";
const string SourceFile2 = "prices.txt";
WebRequest request = WebRequest.Create("https://www.zzap.ru/webservice/datasharing.asmx/GetSearchSuggest");
request.Method = "POST";
string postData = text + i + prob + j + "&" + row + "&" + key;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
string n = responseFromServer;
FileInfo f = new FileInfo(SourceFile); // Записываем потоком сюда.
StreamWriter w = f.CreateText();
w.WriteLine(responseFromServer);
w.Close();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
System.Threading.Thread.Sleep(4000);
string LogFile = "Mytext3.txt";
string codecat = "code_cat";
var txt = new StreamReader("Mytext2.txt");
var text2 = txt.ReadToEnd();
txt.Close();
if (text2.Length > 1000 | text2.Length == 135)
goto cycle;
text2 = text2.Substring(text2.IndexOf(codecat));
text2 = text2.Substring(0, text2.LastIndexOf(',') + 1);
text2 = text2.Substring(text2.IndexOf(':') + 1);
char[] MyChar = { ',' };
char[] MyChar2 = { ':' };
text2 = text2.TrimEnd(MyChar);
FileInfo r = new FileInfo(LogFile); // Записываем потоком сюда.
StreamWriter x = r.CreateText();
x.WriteLine(text2);
x.Close(); // Общая запись.
outWorkSheet.Cells[U - 1, 4] = text2;
WebRequest request2 = WebRequest.Create("https://www.zzap.ru/webservice/datasharing.asmx/GetStatPrices");
request2.Method = "POST";
string postData2 = "codes_cat=" + text2 + "&code_region=1" + "&instock=1" + "&wholesale=0" + "&" + key;
Console.WriteLine(postData2);
byte[] byteArray2 = Encoding.UTF8.GetBytes(postData2);
request2.ContentType = "application/x-www-form-urlencoded";
request2.ContentLength = byteArray2.Length;
Stream dataStream2 = request2.GetRequestStream();
dataStream2.Write(byteArray2, 0, byteArray2.Length);
dataStream2.Close();
WebResponse response2 = request2.GetResponse();
System.Threading.Thread.Sleep(4000);
Console.WriteLine(((HttpWebResponse)response2).StatusDescription);
dataStream2 = response2.GetResponseStream();
StreamReader reader2 = new StreamReader(dataStream2);
string responseFromServer2 = reader2.ReadToEnd();
string n2 = responseFromServer;
FileInfo f2 = new FileInfo(SourceFile2); // Записываем потоком сюда.
StreamWriter w2 = f2.CreateText();
w2.WriteLine(responseFromServer2);
w2.Close();
FileInfo f3 = new FileInfo(SourceFile2); // Записываем потоком сюда.
StreamWriter w3 = f3.CreateText();
w3.WriteLine(responseFromServer2);
w3.Close();
FileInfo f4 = new FileInfo(SourceFile2); // Записываем потоком сюда.
StreamWriter w4 = f4.CreateText();
w4.WriteLine(responseFromServer2);
w4.Close();
string minp = "min.txt";
string medp = "med.txt";
string maxp = "max.txt";
string minprice = "price_min";
string medprice = "price_med";
string maxprice = "price_max";
var txt2 = new StreamReader("prices.txt");
var text3 = txt2.ReadToEnd();
txt2.Close();
if (text3.Length == 124)
goto cycle;
text3 = text3.Substring(text3.IndexOf(minprice));
text3 = text3.Substring(0, text3.LastIndexOf(',') + 1);
text3 = text3.Substring(text3.IndexOf(':') + 1);
text3 = text3.TrimEnd(MyChar);
text3 = text3.Substring(0, text3.LastIndexOf(',') + 1);
text3 = text3.TrimEnd(MyChar);
string min = text3;
FileInfo r2 = new FileInfo(minp); // Записываем потоком сюда.
StreamWriter x2 = r2.CreateText();
x2.WriteLine(min);
x2.Close(); // Общая запись.
var txt3 = new StreamReader("prices.txt");
var text4 = txt3.ReadToEnd();
txt3.Close();
text4 = text4.Substring(text4.IndexOf(medprice));
text4 = text4.Substring(0, text4.LastIndexOf(',') + 1);
text4 = text4.TrimEnd(MyChar);
text4 = text4.Substring(text4.IndexOf(":") + 1);
text4 = text4.TrimEnd(MyChar2);
string mid = text4;
FileInfo r3 = new FileInfo(medp); // Записываем потоком сюда.
StreamWriter x3 = r3.CreateText();
x3.WriteLine(mid);
x3.Close(); // Общая запись.
var txt4 = new StreamReader("prices.txt");
var text5 = txt4.ReadToEnd();
txt4.Close();
text5 = text5.Substring(text5.IndexOf(maxprice));
text5 = text5.Substring(text5.IndexOf(":") + 1);
text5 = text5.Replace("}]}</string>", "");
string max = text5;
FileInfo r4 = new FileInfo(maxp); // Записываем потоком сюда.
StreamWriter x4 = r4.CreateText();
x4.WriteLine(max);
x4.Close(); // Общая запись.
outWorkSheet.Cells[U - 1, 6] = min;
outWorkSheet.Cells[U - 1, 7] = mid;
outWorkSheet.Cells[U - 1, 8] = max;
// задержка между запросами на сервер.
cycle:
System.Threading.Thread.Sleep(200); // задержка между запросами на сервер.
U++;
}
while (U < 9511);
}
}