提问者:小点点

RestSharp使用异步刮取多个URL


检查暗号。这里我得到了一个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);
        }


        }

共1个答案

匿名用户

使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();
}

几点:您需要使mainAsync能够等待对DoscrapeAsync的每次调用。

此外,使用task.run也是完全不必要的,因为这只是将工作卸载到线程池中,在您的情况下什么也没有实现。

相关问题