Results 1 to 25 of 25

Thread: Scraping Links from Web??? help please?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Scraping Links from Web??? help please?

    I am using webbrowser control to login to a webpage and auto submit login, email and password with this code:
    VB Code:
    1. Webbrowser1.Document.All("usrname").Focus()
    2.         Webbrowser1.Document.ActiveElement.InnerText = txt_user.Text
    3.         Webbrowser1.Document.All("uemail").Focus()
    4.         Webbrowser1.Document.ActiveElement.InnerText = txt_email.Text
    5.         Webbrowser1.Document.All("peeword").Focus()
    6.         Webbrowser1.Document.ActiveElement.InnerText = txt_pass.Text
    7.         Webbrowser1.Document.Forms(0).InvokeMember("Submit")

    Yes, I'm a noob but it works so after a long time finding out vb.net 2005 didn't support formname.submit() I was very happy to get this working

    Anyhow, I am now trying to use webbrowser control to scrape links with stats.php?id=6digit#here in it's link. I've tried everything I can possibly think of over the past few days and am lost. ANY help will be much appreciated. Thanks


    Kevin
    Last edited by khuntballa; Jul 29th, 2006 at 08:44 PM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    ^ Sorry to bump but I really am completely lost here if I could get any help I'd love it. Thanks

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    Here is an example that should get you started:

    VB Code:
    1. Dim scrapedLinks As New List(Of HtmlAnchorElement)
    2. Dim allLinks As HtmlElementCollection = Webbrowser1.GetElementsByTagName("a")
    3. For Each link as HtmlAnchorElement In allLinks
    4.   If (link.GetAttribute("href").IndexOf("stats.php?id=6digit#here") > 0) Then
    5.     scrapedLinks.Add(link)
    6.   End If
    7. Next

    Untested, hope the class names are right.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    Thanks for replying, I tried
    VB Code:
    1. Webbrowser1.Navigate("http://www.kingsofchaos.com/battlefield.php?" & "start=" & txt_low.Text)
    2.         Dim scrapedLinks As New List(Of HTMLAnchorElement)
    3.         Dim allLinks As HTMLElementCollection = Webbrowser1.Document.GetElementsByTagName("a")
    4.         For Each link As HTMLAnchorElement In allLinks
    5.             If (link.getAttribute("href").IndexOf("stats.php?id=") > 0) Then
    6.                 scrapedLinks.Add(link)
    7.                    End If
    8.         Next
    without adding an
    VB Code:
    1. Imports System.Collections.Generic
    I got an error list not defined.

    Now the error I am getting is unable to cast object of type system.windows.forms.htmlcollection to type mshtml.htmlelementcollection. I have added the imports statement for mshtml.... any help is apreciated

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    Oh... change HTMLElementCollection to HTMLCollection then.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    htmlcollection is not defined



    Again thanks for helping me penagate
    Last edited by khuntballa; Jul 30th, 2006 at 11:48 AM.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    I was right its HtmlElementCollection.

    VB Code:
    1. Dim allLinks As HtmlElementCollection = Webbrowser1.Document.GetElementsByTagName("a")

    that should work

    http://msdn2.microsoft.com/en-us/lib...bytagname.aspx

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    It is still getting
    unable to cast object of type system.windows.forms.htmlcollection to type mshtml.htmlelementcollection

    Any ideas what could be causing that?
    Last edited by khuntballa; Jul 30th, 2006 at 12:15 PM.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    Well then the documentation is incorrect because that's what they've used

    I'll give it a try

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    It worked for me although I needed to change HtmlAnchorElement to HtmlElement.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    Hrmm,
    I am using 2005 could that be a reason?

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    So am I and so is the documentation which explicitly says it is a new method in the Framework 2.0

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    This is what I am trying w/ a completely new application now
    VB Code:
    1. WebBrowser2.Navigate("www.kingsofchaos.com/battlefield.php")
    2.         Dim scrapedLinks As New List(Of HtmlElement)
    3.         Dim allLinks As HTMLElementCollection = webBrowser2.Document.GetElementsByTagName("a")
    4.         For Each link As HtmlElement In allLinks
    5.             If (link.GetAttribute("href").IndexOf("stats.php?id=") > 0) Then
    6.                 scrapedLinks.Add(link)
    7.             End If
    8.         Next

    And the Error I am getting is Unable to cast object of type system.Windows.forms.htmlelementcollection to type mshtml.htmlelementcollection

    Did you add any additional references? I added mshtml and that was it



    A first chance exception of type 'System.NullReferenceException' occurred in DarkFuryToolz.exe
    A first chance exception of type 'System.NullReferenceException' occurred in DarkFuryToolz.exe
    In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.

    I get this for:

    Dim allLinks As HTMLElementCollection = webBrowser2.Document.GetElementsByTagName("a")
    Last edited by khuntballa; Jul 30th, 2006 at 08:58 PM.

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    bump ^^

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    Quote Originally Posted by khuntballa
    I added mshtml and that was it
    Daah, that's the problem. Take it out

    It's an ambigous reference.

  16. #16

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    Still getting
    Object reference not set to an instance of an object.
    and it is highlighting

    VB Code:
    1. Dim allLinks As HTMLElementCollection = webBrowser2.Document.GetElementsByTagName("a")

  17. #17
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: Scraping Links from Web??? help please?

    You probably don't have a document loaded in webBrowser2. Try adding a check that webBrowser2 has a document.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  18. #18

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    I'll probably sound stupid for asking this, but how do I actually view that list to check it's exact output?

    I tried doing listbox2.items.add(link) in the loop and it came up as htmlelement 50 times

  19. #19
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: Scraping Links from Web??? help please?

    I don't know for sure, but I'd guess a member of link would have what you want... probably value or text or something similar
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  20. #20
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    It needs to be done in the DocumentComplete event

  21. #21

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    15

    Re: Scraping Links from Web??? help please?

    So the last thing before I close this thread, is how do I export that list to something readable by me in thne correct format? like when I tried adding it to a listbox all I got was htmlelement instead of the link

  22. #22
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?

    Make sure you have "Option Strict On" at the top of your code file. This should prevent you from doing things which don't make sense, like listbox2.items.add(link).

    Next, type a dot "." after "link" and you'll see everything you need.

  23. #23
    Junior Member
    Join Date
    Mar 2007
    Posts
    21

    Re: Scraping Links from Web??? help please?

    can u make it in normal VB 6.0??

  24. #24
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Scraping Links from Web??? help please?


  25. #25
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Scraping Links from Web??? help please?

    Quote Originally Posted by penagate
    Nice recommendation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width