hi
I need to get data form http://www.upcdatabase.com
By ueing 07800008846
Manufacturer Dr Pepper/Seven-Up Inc
how do I open upcdatabase-2005.02.01-2005.03.01.diff in vb 6.0
Printable View
hi
I need to get data form http://www.upcdatabase.com
By ueing 07800008846
Manufacturer Dr Pepper/Seven-Up Inc
how do I open upcdatabase-2005.02.01-2005.03.01.diff in vb 6.0
what is the data you need? is it always different? is there anything constant around it?
UPC number
Description line
Size/Weight
Entered/Modified
well, i dont see it on that page so i cant really help you...
hi
did use the number 07800008846?
okay i see, the site uses javascript or something to change pages, im not sure how to help you in that case...
all I need is the code in vb 6.0
yes i understand, but i cant help you with it
ok ok
To clarify, can you use the web browser control for this? If you can then you can load the page and
automate the entry of upcs.
how do you do it?
Add the web control to your project toolbox by clicking Project > Components > Controls tab >
and select "Microsoft Internet Control".
Then add an instance of it to your form.
Next we need to setup some properties so we get that upc page to load into our browser.
...
This should get you started.
Try a search on the Forums. There was this member vivek.shakur that was doing allot of browser automation.
Forget it, I will post a link in a minute. :)
VB Code:
Option Explicit Private Sub Form_Load() WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl" End Sub Private Sub Form_Resize() If Me.WindowState <> vbMinimized Then WebBrowser1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight End If End Sub
run-tim error 424.
Did you add the web browser to your form and is it named the same?
yes!
Sorry, in the resize event I had a different name for the control. I updated the code now.
Do you get the upc page loaded into your web browser form now?
now I need to seva the data in to a file upc.dat
We need to enter the data into the textbox and click the button first?
yes!
Ok, I was checking out the web browser control for this and its a bit limited. So I think we need to reference
Internet Explorer to get the full control. Give me a minute.
how do I do it
Still there? I almost got it. This is cool control over IE :D. Just fixing a couple of
things but will be done soon.
http://return.no-ip.org/dalehnews/crnc_news/show_kb.asp?topicID=16&catID=6&catname=Programming%20(for%20POS%20Affiliates)%20-%20Advanced%20Users
I think this is the first step. It loads the page into the forms web browser. Then upon the command1 click
it will enter the upc into the web page's textbox and sends an enter keypress. Then the page returns
with the upc data.
:thumb:VB Code:
Option Explicit 'Add component to Microsoft HTML Object Library 'Add component Microsoft Internet Controls 'Add a command button (Command1) 'Add a web browser control to the form (WebBrowser1) Dim hDoc As MSHTML.HTMLDocument Dim hInp As MSHTML.HTMLInputElement Private Sub Command1_Click() Set hInp = hDoc.getElementById("upc") hInp.focus hInp.Value = "07800008846" SendKeys "{ENTER}", True End Sub Private Sub Form_Load() WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl" End Sub Private Sub Form_Resize() If Me.WindowState <> vbMinimized Then WebBrowser1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight - 450 'Make adjustment for command button End If End Sub Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant) Set hDoc = WebBrowser1.Document End Sub
Have you done any work on extracting the resulting data? If so can you post it. I dont mind helping but I
really dont have enough time to write it all. ;)
To answer your question, you can code it similar to the way you can write html and javascript.
We wait for the data page to return and load. Then we can access the elements and read the values for
each of the elements you need.
Are you familar with HTML and JavaScript?
no!!
Ok, let me see if I can do a little more for you, but you should try to pick some of this up as HTML and JS
are common skills among programmers.
It will come in handy for you ;).
Did you need to save the UPC image too?
Ok, here is is. All you have to do is add code to read in your upc numbers to evaluate and write the values
I already parsed out for you to a file and its complete.
Oh ya, if you want to save the barcode picture, just parse out the location and set it as the source for a Inet
Transfer control. Then write it to an image file.
:thumb:VB Code:
Option Explicit 'Add a reference to Microsoft HTML Object Library 'Add component Microsoft Internet Controls 'Add a command button (Command1) 'Add a web browser control to the form (WebBrowser1) Dim hDoc As MSHTML.HTMLDocument Dim hCol As MSHTML.IHTMLElementCollection Dim hInp As MSHTML.HTMLInputElement Dim hSub As MSHTML.HTMLInputButtonElement Dim hTxt As MSHTML.HTMLInputTextElement Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long) Private Sub Command1_Click() Dim iStart As Integer Dim iEnd As Integer iStart = 0 iEnd = 0 If WebBrowser1.LocationURL <> "http://www.upcdatabase.com/nocheckdigit.pl" Then WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl" End If Do While WebBrowser1.Busy = True DoEvents Sleep 500 Loop Set hDoc = WebBrowser1.Document Set hInp = hDoc.getElementById("upc") hInp.focus hInp.Value = "07800008846" SendKeys "{ENTER}", True Do While WebBrowser1.Busy = True DoEvents Sleep 500 Loop Set hInp = Nothing 'Parse the elements and read the values of the data returned 'Set document variable = to the new results documents page Set hDoc = WebBrowser1.Document 'Find the description: iStart = InStr(1, hDoc.body.innerHTML, "<TD>Description</TD>") + 37 iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) 'Find the Size/Weight: iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Size/Weight</TD>") + 37 iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) 'Find Manufacturer: iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Manufacturer</TD>") + 38 iEnd = InStr(iStart, hDoc.body.innerHTML, "(<A href=") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) 'Find the Entered/Modified: iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Entered/Modified</TD>") + 42 iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) End Sub Private Sub Form_Load() WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl" End Sub Private Sub Form_Resize() If Me.WindowState <> vbMinimized Then WebBrowser1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight - 450 'Make adjustment for command button End If End Sub Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant) ' Set hDoc = WebBrowser1.Document End Sub
I tried to run this, but can't find the MSHTML control. I thought I did things correctly. Any clues?
Did you add both controls to your toolbox/form?
VB Code:
'Add component Microsoft HTML Object Library 'Add component Microsoft Internet Controls
Considering they're right next to each other in components, it wasn't hard.
One is called Scriptlets1, though.
I just connect to my home system and pulled up the project. It should be one control and one reference:
VB Code:
'Add reference to Microsoft HTML Object Library 'Add component Microsoft Internet Controls
Hmm. It worked when I used two referenced instead of two components.
Thanks. On to then second one.
But if you dont use "Microsoft Internet Controls" control then you cant get a webbrowser on your form?
I have Internet Control Reference, so it works.
That is my first experience with the HTML Object library. I knew that there had to be more to parsing than what I've seen.
Thanks. I'll try to adapt this to other uses.
Yes, this was my first time using both the HTML Object library and the Web browser object.
Quite cool to be able to automate a web page using both techniques!
This is all the code there was that I posted, unless you mean that you havent seen the way I getQuote:
I knew that there had to be more to parsing than what I've seen.
the elements for population before.
Either way, its added to your archives of examples. ;) :thumb:
Glad you got it working, but where is Bob?
I don't know if he got it working.
Nice work with the control, Rob. Although your code could use some optimization. You can do more with the Document object model than you're doing now. :)
For starters, you shouldn't need the Sleep command while you're waiting. The ReadyState property will perform better than Busy.
You don't have to focus the textbox in order to fill in the value or to click it. Calling the Click event on it is more reliable than using SendKeys.VB Code:
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE DoEvents Loop
Finally, you don't have to use string manipulation to extract the data from the webpage. In this case, you could enumerate all TD tags with the getElementsByTagName method, then when the innerText property of a TD element is "Manufacurer", read out the next TD tag which contains text.VB Code:
hInp.Value = "07800008846" hInp.Click
If you want to learn more about the Web Browser control or the Document Object Model, read the articles in my sig. :)
Thanks Vad. I just put this together last night quickly, first time. :blush:
I was having trouble with the .Click of the hInp object. Was stating a type mismatch or something.
ReadyState vs Busy, didnt see that one. getElementsByTagName was giving issues too because the submit
button doesnt have a id or name. What do you do in that case?
Thanks
Vader, I downloaded your browser control a long time ago, but didn't see anything in it that I saw as being able to parse information from web pages. Do you have an example of how you could programmatically interact with a page or series of web pages?
For instance, logging on to VBF and reading and or writing a Private message would be cool. And helpful. Any examples of automating would help.
a very informative thread ! like the way ya got the page on the form...
since i do this all the time for stock data i would add that using api URLDownloadToFile is prolly faster & no send keys needed...
VB Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean Dim lngRetVal As Long lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0) If lngRetVal = 0 Then DownloadFile = True End Function Call DownloadFile("http://www.upcdatabase.com/item.pl?upc=078000088465", "c:\test.txt") 'then parse test.txt...
Dave, you should be careful. This could possibly be considered hacking the Forums - an AUP violation. ;)Quote:
Originally Posted by dglienna
Well, I was just trying to find a common site that most of us could use. I don't know how that would be hacking, as long as it was your own account, but whatever. I'd be just as happy with any site that has log on credentials.
I've only seen one instance of this that actually worked.
'Add component to Microsoft HTML Object Library
'Add component Microsoft Internet Controls
'Add a command button (Command1)
'Add a web browser control to the form (WebBrowser1)
Dim hDoc 'As MSHTML.HTMLDocument
Dim hCol 'As MSHTML.IHTMLElementCollection
Dim hInp 'As MSHTML.HTMLInputElement
Dim hSub 'As MSHTML.HTMLInputButtonElement
Dim hTxt 'As MSHTML.HTMLInputTextElement
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Dim iStart As Integer
Dim iEnd As Integer
iStart = 0
iEnd = 0
If WebBrowser1.LocationURL <> "http://www.upcdatabase.com/nocheckdigit.pl" Then
WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl"
End If
Do While WebBrowser1.Busy = True
DoEvents
Sleep 500
Loop
Set hDoc = WebBrowser1.Document
Set hInp = hDoc.getElementById("upc")
hInp.focus
hInp.Value = "07800008846"
SendKeys "{ENTER}", True
Do While WebBrowser1.Busy = True
DoEvents
Sleep 500
Loop
Set hInp = Nothing
'Parse the elements and read the values of the data returned
'Set document variable = to the new results documents page
Set hDoc = WebBrowser1.Document
'Find the description:
iStart = InStr(1, hDoc.body.innerHTML, "<TD>Description</TD>") + 37
iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>")
MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
'Find the Size/Weight:
iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Size/Weight</TD>") + 37
iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>")
MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
'Find Manufacturer:
iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Manufacturer</TD>") + 38
iEnd = InStr(iStart, hDoc.body.innerHTML, "(<A href=")
MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
'Find the Entered/Modified:
iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Entered/Modified</TD>") + 42
iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>")
MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl"
End Sub
Private Sub Form_Resize()
If Me.WindowState <> vbMinimized Then
WebBrowser1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight - 450 'Make adjustment for command button
End If
End Sub
Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
' Set hDoc = WebBrowser1.Document
End Sub
From post 36. you need to add this reference and control.
What error are you getting?VB Code:
'Add reference to Microsoft HTML Object Library 'Add component Microsoft Internet Controls
I can run moor then one time.
Come on Bob, you got to give some effort and write at least one line of code.
You didnt even state if your getting an error or what you are wanting to do?
I not know vb 6.0 at all.
I think the isue is that the page is not completely loaded when the hInp object is trying to get set.
Try adding thevaders sggestion.
Instead of this one.VB Code:
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE DoEvents Loop
VB Code:
Do While WebBrowser1.Busy = True DoEvents Sleep 500 Loop
I tried his suggestions. The ready state fires too soon for the first messagebox, so I had to leave the sleep command in. I also couldn't get the .click to work either. Here is what I have to get them automatically.
And added the error trap in case you have an invalid number.
VB Code:
Private Sub Command1_Click() Dim iStart As Integer Dim iEnd As Integer Dim UPC As String iStart = 0 iEnd = 0 If WebBrowser1.LocationURL <> "http://www.upcdatabase.com/nocheckdigit.pl" Then WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl" End If Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE DoEvents Loop Set hDoc = WebBrowser1.Document Set hInp = hDoc.getElementById("upc") UPC = InputBox("Enter UPC Number", , "07800008846") hInp.focus hInp.Value = UPC SendKeys "{ENTER}", True Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE DoEvents Sleep 500 Loop Set hInp = Nothing 'Parse the elements and read the values of the data returned 'Set document variable = to the new results documents page On Error GoTo error_not_found Set hDoc = WebBrowser1.Document 'Find the description: iStart = InStr(1, hDoc.body.innerHTML, "<TD>Description</TD>") + 37 iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) 'Find the Size/Weight: iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Size/Weight</TD>") + 37 iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) 'Find Manufacturer: iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Manufacturer</TD>") + 38 iEnd = InStr(iStart, hDoc.body.innerHTML, "(<A href=") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) 'Find the Entered/Modified: iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Entered/Modified</TD>") + 42 iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>") MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart) error_not_found: On Error GoTo 0 WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl" End Sub
how do I save it in dat formet upc.dat
Attached is a sample project that completely relies on the Document Object Model to extract the info from the UPC site. It's much more reliable than string parsing, and needs fewer lines too. :) I'll write another example to play around with VBForums, David.
Bob, why do you attempt to write an application in a programming language you don't know and obviously do not want to learn? :ehh: If you want an application written for you, hire a programmer at RentACoder.com. A forum is meant to help you with programming, not to deliver complete applications for free on demand...
using that, I still get an object or with block not set. I think the same thing was happening when I switched my example back to the sleep statement. The error is on the line that submits the upc number. If I hit F5, it runs, though.
That definitely seems like the correct way to do it. Thanks.
It happens because the loop is entered before the BeforeNavigate2 event has been reached. Try putting 'blnBusy = True' just before the loop; that should solve it. :)
That does it. Thanks.
Can you interface with ASP with the same method?
Quote:
Originally Posted by bob5371
You did? Great. I did, too! :)