检查暗号。这里我得到了一个URL
的列表,它们需要报废,并将Html填充到data.datas
全局类的Html
属性中。我已经尝试过使用RestSharpClient.GetAsync
来实现它,但我不知道如何才能将它转到URL
的所有列表中,其中包含data.datas
public class Data
{
public int Id { get; set; }
public string Url { get; set; }
public string Html { get; set; }
public static List<Data> Datas = new List<Data>();
}
class Program
{
static void Main(string[] args)
{
//seeding 10 demo data
for (int i = 0; i < 10; i++)
{
Data.Datas.Add(new Data
{
Id = i,
Url = "https://www.httpbin.org",
Html = null,
});
}
var task = Task.Run(async () =>
{
await DoScrapeAsync();
});
do
{
} while (!task.IsCompleted);
Console.WriteLine("compleated");
Console.ReadLine();
}
static async Task DoScrapeAsync()
{
var thisData = Data.Datas.FirstOrDefault();//doing firstOrDefault() which is wrong. I have to take whole list to scrape
var client = new RestClient("http://api.scraperapi.com/?api_key=c3df2_fake_4d5e&url=" + thisData.Url + "/ip&country_code=us");
RestRequest req = new RestRequest(Method.GET);
//string html = client.Execute<string>(req).Content;
var html = await client.GetAsync<string>(req);
thisData.Html = html;
Console.WriteLine(html);
}
}
使doscrapeasync
接受单个data
实例:
static async Task DoScrapeAsync(Data data)
{
string url = $"http://api.scraperapi.com/?api_key=c3df2_fake_4d5e&url={data.Url}/ip&country_code=us";
var client = new RestClient(url);
RestRequest req = new RestRequest(Method.GET);
var html = await client.GetAsync<string>(req);
data.Html = html;
Console.WriteLine(html);
}
然后可以在main
中这样调用:
static async Task Main(string[] args)
{
//seeding 10 demo data
for (int i = 0; i < 10; i++)
{
Data.Datas.Add(new Data
{
Id = i,
Url = "https://www.httpbin.org",
Html = null,
});
}
await Task.WhenAll(Data.Datas.Select(DoScrapeAsync));
Console.WriteLine("completed");
Console.ReadLine();
}
几点:您需要使main
Async
能够等待对DoscrapeAsync
的每次调用。
此外,使用task.run
也是完全不必要的,因为这只是将工作卸载到线程池中,在您的情况下什么也没有实现。