VBA Adjustment for adaptablity
I have this code I would like to rewrite to get the Bid price from yahoo. The code currently gets the Last price, however I would like to get the Bid price and if the Bid price is zero, then get the last price. I tried exhaustively to rewrite it myself but was not successful. Can someone assist in my quest to rewrite this code.
Thank you kindly
Sub GetRate()
Dim XMLPage As New MSXML2.XMLHTTP60
Dim htmlDoc As New MSHTML.HTMLDocument
Dim URL As String
Dim HTMLspans As MSHTML.IHTMLElementCollection
Dim HTMLspan As MSHTML.IHTMLElement
URL = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
XMLPage.Open "GET", URL, False
XMLPage.send
htmlDoc.body.innerHTML = XMLPage.responseText
Set HTMLspans = htmlDoc.getElementsByTagName("span")
For Each HTMLspan In HTMLspans
If HTMLspan.className = "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" Then
debug.Print HTMLspan.innerText
End If
Next HTMLspan
End Sub
web web-scraping yahoo-finance
add a comment |
I have this code I would like to rewrite to get the Bid price from yahoo. The code currently gets the Last price, however I would like to get the Bid price and if the Bid price is zero, then get the last price. I tried exhaustively to rewrite it myself but was not successful. Can someone assist in my quest to rewrite this code.
Thank you kindly
Sub GetRate()
Dim XMLPage As New MSXML2.XMLHTTP60
Dim htmlDoc As New MSHTML.HTMLDocument
Dim URL As String
Dim HTMLspans As MSHTML.IHTMLElementCollection
Dim HTMLspan As MSHTML.IHTMLElement
URL = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
XMLPage.Open "GET", URL, False
XMLPage.send
htmlDoc.body.innerHTML = XMLPage.responseText
Set HTMLspans = htmlDoc.getElementsByTagName("span")
For Each HTMLspan In HTMLspans
If HTMLspan.className = "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" Then
debug.Print HTMLspan.innerText
End If
Next HTMLspan
End Sub
web web-scraping yahoo-finance
add a comment |
I have this code I would like to rewrite to get the Bid price from yahoo. The code currently gets the Last price, however I would like to get the Bid price and if the Bid price is zero, then get the last price. I tried exhaustively to rewrite it myself but was not successful. Can someone assist in my quest to rewrite this code.
Thank you kindly
Sub GetRate()
Dim XMLPage As New MSXML2.XMLHTTP60
Dim htmlDoc As New MSHTML.HTMLDocument
Dim URL As String
Dim HTMLspans As MSHTML.IHTMLElementCollection
Dim HTMLspan As MSHTML.IHTMLElement
URL = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
XMLPage.Open "GET", URL, False
XMLPage.send
htmlDoc.body.innerHTML = XMLPage.responseText
Set HTMLspans = htmlDoc.getElementsByTagName("span")
For Each HTMLspan In HTMLspans
If HTMLspan.className = "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" Then
debug.Print HTMLspan.innerText
End If
Next HTMLspan
End Sub
web web-scraping yahoo-finance
I have this code I would like to rewrite to get the Bid price from yahoo. The code currently gets the Last price, however I would like to get the Bid price and if the Bid price is zero, then get the last price. I tried exhaustively to rewrite it myself but was not successful. Can someone assist in my quest to rewrite this code.
Thank you kindly
Sub GetRate()
Dim XMLPage As New MSXML2.XMLHTTP60
Dim htmlDoc As New MSHTML.HTMLDocument
Dim URL As String
Dim HTMLspans As MSHTML.IHTMLElementCollection
Dim HTMLspan As MSHTML.IHTMLElement
URL = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
XMLPage.Open "GET", URL, False
XMLPage.send
htmlDoc.body.innerHTML = XMLPage.responseText
Set HTMLspans = htmlDoc.getElementsByTagName("span")
For Each HTMLspan In HTMLspans
If HTMLspan.className = "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" Then
debug.Print HTMLspan.innerText
End If
Next HTMLspan
End Sub
web web-scraping yahoo-finance
web web-scraping yahoo-finance
edited Nov 21 '18 at 9:54
Dmitriy Fialkovskiy
1,57521325
1,57521325
asked Nov 21 '18 at 9:25
AnthonyAnthony
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Try the following. It should fetch you the bid price if it is greater than 0 otherwise it will grab you the last price:
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim S$, elem As Object, post As Object
With New XMLHTTP60
.Open "GET", Url, False
.send
S = .responseText
End With
With New HTMLDocument
.body.innerHTML = S
Set elem = .querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End With
End Sub
Exactly the way you have tried above other than .querySelector():
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim Http As New XMLHTTP60, Htmldoc As New HTMLDocument
Dim elem As Object, post As Object
With Http
.Open "GET", Url, False
.send
Htmldoc.body.innerHTML = .responseText
End With
Set elem = Htmldoc.querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = Htmldoc.querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End Sub
Reference to add to the library:
Microsoft xml,v6.0
Microsoft Html Object Library
If you wanna learn how .querySelector() works, check out this link.
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
Did you try wrapping the script withinApplication.ScreenUpdating = FalseandApplication.ScreenUpdating = Trueblock to see the performance?
– SIM
Nov 21 '18 at 12:59
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408853%2fvba-adjustment-for-adaptablity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try the following. It should fetch you the bid price if it is greater than 0 otherwise it will grab you the last price:
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim S$, elem As Object, post As Object
With New XMLHTTP60
.Open "GET", Url, False
.send
S = .responseText
End With
With New HTMLDocument
.body.innerHTML = S
Set elem = .querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End With
End Sub
Exactly the way you have tried above other than .querySelector():
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim Http As New XMLHTTP60, Htmldoc As New HTMLDocument
Dim elem As Object, post As Object
With Http
.Open "GET", Url, False
.send
Htmldoc.body.innerHTML = .responseText
End With
Set elem = Htmldoc.querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = Htmldoc.querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End Sub
Reference to add to the library:
Microsoft xml,v6.0
Microsoft Html Object Library
If you wanna learn how .querySelector() works, check out this link.
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
Did you try wrapping the script withinApplication.ScreenUpdating = FalseandApplication.ScreenUpdating = Trueblock to see the performance?
– SIM
Nov 21 '18 at 12:59
|
show 1 more comment
Try the following. It should fetch you the bid price if it is greater than 0 otherwise it will grab you the last price:
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim S$, elem As Object, post As Object
With New XMLHTTP60
.Open "GET", Url, False
.send
S = .responseText
End With
With New HTMLDocument
.body.innerHTML = S
Set elem = .querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End With
End Sub
Exactly the way you have tried above other than .querySelector():
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim Http As New XMLHTTP60, Htmldoc As New HTMLDocument
Dim elem As Object, post As Object
With Http
.Open "GET", Url, False
.send
Htmldoc.body.innerHTML = .responseText
End With
Set elem = Htmldoc.querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = Htmldoc.querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End Sub
Reference to add to the library:
Microsoft xml,v6.0
Microsoft Html Object Library
If you wanna learn how .querySelector() works, check out this link.
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
Did you try wrapping the script withinApplication.ScreenUpdating = FalseandApplication.ScreenUpdating = Trueblock to see the performance?
– SIM
Nov 21 '18 at 12:59
|
show 1 more comment
Try the following. It should fetch you the bid price if it is greater than 0 otherwise it will grab you the last price:
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim S$, elem As Object, post As Object
With New XMLHTTP60
.Open "GET", Url, False
.send
S = .responseText
End With
With New HTMLDocument
.body.innerHTML = S
Set elem = .querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End With
End Sub
Exactly the way you have tried above other than .querySelector():
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim Http As New XMLHTTP60, Htmldoc As New HTMLDocument
Dim elem As Object, post As Object
With Http
.Open "GET", Url, False
.send
Htmldoc.body.innerHTML = .responseText
End With
Set elem = Htmldoc.querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = Htmldoc.querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End Sub
Reference to add to the library:
Microsoft xml,v6.0
Microsoft Html Object Library
If you wanna learn how .querySelector() works, check out this link.
Try the following. It should fetch you the bid price if it is greater than 0 otherwise it will grab you the last price:
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim S$, elem As Object, post As Object
With New XMLHTTP60
.Open "GET", Url, False
.send
S = .responseText
End With
With New HTMLDocument
.body.innerHTML = S
Set elem = .querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End With
End Sub
Exactly the way you have tried above other than .querySelector():
Sub GetRate()
Const Url$ = "https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000"
Dim Http As New XMLHTTP60, Htmldoc As New HTMLDocument
Dim elem As Object, post As Object
With Http
.Open "GET", Url, False
.send
Htmldoc.body.innerHTML = .responseText
End With
Set elem = Htmldoc.querySelector("td[data-test='BID-value'] > span")
If elem.innerText = 0 Then
Set post = Htmldoc.querySelector("#quote-market-notice").ParentNode.FirstChild
MsgBox post.innerText
Else: MsgBox elem.innerText
End If
End Sub
Reference to add to the library:
Microsoft xml,v6.0
Microsoft Html Object Library
If you wanna learn how .querySelector() works, check out this link.
edited Nov 21 '18 at 10:28
answered Nov 21 '18 at 10:10
SIMSIM
10.2k3743
10.2k3743
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
Did you try wrapping the script withinApplication.ScreenUpdating = FalseandApplication.ScreenUpdating = Trueblock to see the performance?
– SIM
Nov 21 '18 at 12:59
|
show 1 more comment
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
Did you try wrapping the script withinApplication.ScreenUpdating = FalseandApplication.ScreenUpdating = Trueblock to see the performance?
– SIM
Nov 21 '18 at 12:59
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
I sincerely thank you for assisting my request. If I may ask some question as I'm still a novice in vba, What are the lines below accomplish? With New HTMLDocument .body.innerHTML = S Set elem = .querySelector("td[data-test='BID-value'] > span") If elem.innerText = 0 Then Set post = .querySelector("#quote-market-notice").ParentNode.FirstChild
– Anthony
Nov 21 '18 at 10:21
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Check out the edit @Anthony. The way you have targeted the class name in your script is error prone as they are compound class names and most probably dynamic and as a result the script will fail miserably when you try few days from now.
– SIM
Nov 21 '18 at 10:38
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
Awesome....Thank you sooo much! And thank you for the link!
– Anthony
Nov 21 '18 at 10:41
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
is there a way to speed up the code? In my code I posted, I modified it to use with multiple rows (10-rows only), However, when I implemented this new code, it runs significantly slower. I really appreciated your help; I was curious if the code could run faster. Comparing the two codes, there is a significant speed difference. Thanks you for your help.
– Anthony
Nov 21 '18 at 12:20
Did you try wrapping the script within
Application.ScreenUpdating = False and Application.ScreenUpdating = True block to see the performance?– SIM
Nov 21 '18 at 12:59
Did you try wrapping the script within
Application.ScreenUpdating = False and Application.ScreenUpdating = True block to see the performance?– SIM
Nov 21 '18 at 12:59
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408853%2fvba-adjustment-for-adaptablity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown