Automatically extract website data to a spreadsheet

Corried

Board Regular
Joined
Dec 19, 2019
Messages
217
Office Version
  1. 2019
Platform
  1. Windows
  2. Web
Hello, Thank you for sharing..

I am looking for courses on how to automatically extract data from a website to an excel spreadsheet.

The excel file contains fifty links. Each link has the same process, to create a stock market report for fifty companies. Use macro and chrome to generate data.

Can anyone show me a step by step process on how I can do this?

Please watch the video and Excel workbook below to learn more.



I hope you all understand.

Cheers
 
The MSN site has a new popup window; I will have to alter the code to programmatically close it…
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I solved the popup issue by opening a new tab, where it does not load.

For our last test, the partial code below should be enough.

If it breaks, tell me the line.

VBA Code:
Public driver As New ChromeDriver

Sub Money29b()
Dim pt As WebElement, key As New Selenium.Keys, s$
driver.get "https://www.msn.com/en-us/money/stockdetails/fi-bjqejc?duration=1D"
Application.Wait Now + TimeValue("0:00:16")
driver.ExecuteScript "window.open('https://www.msn.com/en-us/money/stockdetails/fi-bjqejc?duration=1D','_blank');"
driver.SwitchToNextWindow
Application.Wait Now + TimeValue("0:00:26")
Set pt = driver.FindElementByXPath("//*[@id=""searchBox""]/input")
pt.Click
pt.SendKeys ActiveSheet.[c2]
Application.Wait Now + TimeValue("0:00:06")
DoEvents
Set pt = driver.FindElementByCss("#searchBox > span > span > svg")
pt.Click
Application.Wait Now + TimeValue("0:00:06")
DoEvents
[a2] = driver.URL
[b2] = driver.FindElementByCss("#root > div:nth-child(1) > div > div.mainContentLayout-DS-EntryPoint1-1 > div > div >" & _
" div:nth-child(1) > div > div.firstSection-DS-EntryPoint1-1 > div > div.title-DS-EntryPoint1-1 > span.displayNameWithBtn-DS-EntryPoint1-1").Text
[d2] = driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[3]/div/div[2]/div[7]/div[2]").Text
[e2] = driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[1]/div/div[4]/div[1]/div/div[1]").Text
'[e2] = driver.FindElementByCss("#root > div:nth-child(1) > div > div.mainContentLayout-DS-EntryPoint1-1 >" & _
" div > div > div:nth-child(1) > div > div.secondSection-DS-EntryPoint1-1 > div.priceInfo-DS-EntryPoint1-1 > div >" & _
" div.mainPrice.color_red-DS-EntryPoint1-1").Text
s = driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[3]/div/div[1]/div[2]/div/div[3]/span[2]").Text
's = driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[2]/div/div[2]/div[1]/div[2]/div/div[3]/span[2]").Text
s = s & " / " & driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[3]/div/div[1]/div[2]/div/div[3]/span[1]").Text
[h2] = s
[j2] = driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[3]/div/div[2]/div[4]/div[2]").Text
' 5-year
driver.FindElementByXPath("//*[@id=""root""]/div[1]/div/div[5]/div/div/div[2]/div/div[1]/div[2]/div/div/button[8]").Click
Application.Wait Now + TimeValue("0:00:06")
MsgBox "End of first part"
End Sub
 
Upvote 0
solved the popup issue by opening a new tab, where it does
Good day Worf. I am sorry. I am not sure if you got my message. Your last code have worked successfully. The only problem the code did not pasted all the data in the cells. The data was only paste for one company...

I was not sure if you design the code to worked for testing. But I am glad not to see any errors involved... Thanks
 
Upvote 0
Yes, that is only test code. We need to run it again in a couple of days to see if it breaks, unfortunately I think it will.

My theory is that the HTML source code continuously changes.
 
Upvote 0
Yes, that is only test code. We need to run it again in a couple of days to see if it breaks, unfortunately I think it will.

My theory is that the HTML source code continuously changes.
Hello Worf.

I did no see ofdjhi
Yes, that is only test code. We need to run it again in a couple of days to see if it breaks, unfortunately I think it will.

My theory is that the HTML source code continuously changes.
 
Upvote 0
Yes, that is only test code. We need to run it again in a couple of days to see if it breaks, unfortunately I think it will.

My theory is that the HTML source code continuously changes.
Hello Worf.

I am sorry, yesterday while typing my keyboard, not functioning the way it should suddenly.
Please forgive me. I have not seen your email notification since last Tuesday...
I have tested the macro again. It's functioning, but still missing some pieces of the puzzle.
 
Upvote 0
I ran it today and got an error at D2.
I will try to find out what is happening during the week.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,453
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top