web scraping to fill out (and retrieve) search forms?

前端 未结 4 1681
孤独总比滥情好
孤独总比滥情好 2021-01-02 17:49

I was wondering if it is possible to \"automate\" the task of typing in entries to search forms and extracting matches from the results. For instance, I have a list of journ

4条回答
  •  粉色の甜心
    2021-01-02 18:08

    WebRequest req = WebRequest.Create("http://www.URLacceptingPOSTparams.com");
    
    req.Proxy = null;
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    
    //
    // add POST data
    string reqString = "searchtextbox=webclient&searchmode=simple&OtherParam=???";
    byte[] reqData = Encoding.UTF8.GetBytes (reqString);
    req.ContentLength = reqData.Length;
    //
    // send request
    using (Stream reqStream = req.GetRequestStream())
      reqStream.Write (reqData, 0, reqData.Length);
    
    string response;
    //
    // retrieve response
    using (WebResponse res = req.GetResponse())
    using (Stream resSteam = res.GetResponseStream())
    using (StreamReader sr = new StreamReader (resSteam))
      response = sr.ReadToEnd();
    
    // use a regular expression to break apart response
    // OR you could load the HTML response page as a DOM 
    

    (Adapted from Joe Albahri's "C# in a nutshell")

提交回复
热议问题