1

This source code scraping is not working; it's giving a blank text file as output:

private void button2_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate("https://www.jigsaw.com/SearchCompany.xhtml?opCode=refresh&rpage=20&order=0&orderby=0&industry=1160000&subindustry=1160300&country=9000&country=2000&cmDead=false&count=0&screenNameType=0&screenName=&omitScreenNameType=0&omitScreenName=&rowsPerPage=200&uid=13473859&tok=1354716874406-8761960955252771794");

    string MainsourceCode = webBrowser1.DocumentText;
    StreamWriter sw = new StreamWriter("G:/jigsaw_info.txt", true);
    sw.Write(MainsourceCode + "\n");
    sw.Close();
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2

You are starting to write too fast. You should wait DocumentCompleted event.

private void button2_Click(object sender, EventArgs e) 
{ 
    webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    webBrowser1.Navigate("https://www.jigsaw.com/SearchCompany.xhtml?opCode=refresh&rpage=20&order=0&orderby=0&industry=1160000&subindustry=1160300&country=9000&country=2000&cmDead=false&count=0&screenNameType=0&screenName=&omitScreenNameType=0&omitScreenName=&rowsPerPage=200&uid=13473859&tok=1354716874406-8761960955252771794");
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
      File.WriteAllText("G:/jigsaw_info.txt",webBrowser1.DocumentText);    
}   
L.B
  • 114,136
  • 19
  • 178
  • 224
  • can you be more clear, Do u want me to move all the above code in those braces? sorry i'm new to coding. please help –  Dec 05 '12 at 16:32
  • `private void button2_Click(object sender, EventArgs e) { webBrowser1.Navigate("URL"); string MainsourceCode = webBrowser1.DocumentText; webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted; void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { File.WriteAllText("G:/jigsaw_info.txt",webBrowser1.DocumentText); } } } }` –  Dec 05 '12 at 16:50
  • @SantoshKumar You sould attach to event before `Navigate` See my new code above. It works. – L.B Dec 05 '12 at 16:53
  • @SantoshKumar That's all I can do. I gave a full working code. I hope you get better answers. – L.B Dec 05 '12 at 16:58
0

You have to wait until navigation is complete. Navigation is asynchronous.

Detect WebBrowser complete page loading

Community
  • 1
  • 1
RQDQ
  • 15,461
  • 2
  • 32
  • 59