Excel Web Query — What in the world is that? If you are like the other 99.9% of MS Excel users, you probably have never heard of microsoft excel web queries (note: statistic made up).
Excel web queries are powerful! Web queries are basically like having a web browser built into Excel that attempts to format the content, putting individual pieces of data into separate cells. You can then use Excel formulas (like =A1/B2) to work directly with the data you’ve downloaded. And you don’t have to know anything about perl, cgi, php, javascript, etc.
Getting Started with Sample Excel Web Queries
The example web queries that come with Excel are usually for importing stock quote data into Excel. For more information about using web queries to download Stock Quotes into Excel, take a look at the Excel Stock Quotes Template.
Although importing stock quotes into Excel happened to be the reason I came across Excel web queries, I have since found many other ways to use them.
It is not the web query itself that is the secret that this article is about. Instead, it’s the ability to make a DYNAMIC Web Query!
By dynamic, I’m not talking about using «refresh» to update the data. What I mean is the ability to change the parameters of the web query, such as typing in a new stock symbol into a cell and having the table update automatically.
If you find yourself using the internet to gather data by filling out forms and copying and pasting data over and over, an excel web query might be the solution to your monotonous woes. It’s not always going to be better, and it’s not always going to work, but it’s worth a try, especially since a simple web query could boost your productivity!
Excel Web Query «.iqy» Files
The real key to creating a dynamic excel web query is to create your own «.iqy» file. In it’s basic form, the «.iqy» file is simply a TEXT file with three main lines:
WEB 1 https://www.thedomain.com/script.pl?paramname=value¶m2=value2
You can create the file using a simple text editor! Most text editors will save a new file with the .txt extension, but you can change the extension to .iqy after you create your text file. If it is already a .iqy file and you want to edit it, you may need to right-click on it to select NotePad or WordPad (or some other text editor) to open it.
The third line is the important one! It’s simply the URL or web page address. Notice that it contains two parameters. If you don’t know what parameters are for, just browse the web for a while and pay attention to what shows up in the address bar of your browser. Go to Google.com for instance, and look up «excel web query».
Parameter name / value pairs are listed after the «?» in the URL and are separated by an «&«.
Make the Web Query Dynamic
To make the query dynamic, replace the value of each parameter in the web query file (queryname.iqy) with:
["paramname","Enter the value for paramname:"]
Want to see how this would apply to a Google search? The form that I used above consists of HTML code that looks like this:
<form action="https://www.google.com/search">
<input type="text" name="q" value="excel web query">
<input type="submit" value="Search Google">
</form>
Notice that «q» is the name of the parameter, and the action tells you what the URL should be. The dynamic web query file for a simple google search would look like this:
WEB 1 https://www.google.com/search?q=["keyword","Enter the Search Term:"]
Let’s Create a Web Query
- Open up a text editor and copy the Google example. Save the file as GoogleSearch.iqy
- Open up Excel and enter a search term in Cell B3
- Open the web query file you just saved (GoogleSearch.iqy) by double-clicking on the file, or open Excel and go to Data > Existing Connections > Browse for More.
- When prompted for the search term, enter =B3. If that doesn’t work, just enter a search term for now.
Play around with the web query options and properties by right-clicking within the query results and selecting Parameters or Data Range Properties or Edit Query.
When you go to Edit Query, you can choose to select a specific portion or table within the page or the entire page (by clicking on one of the yellow arrow boxes in the preview). Then, you can save your new query as a .iqy file and look at what parameters it uses.
WEB 1 https://www.google.com/search?q=["q","Enter the Search Term:"] Selection=EntirePage Formatting=RTF PreFormattedTextToColumns=True ConsecutiveDelimitersAsOne=True SingleBlockTextImport=False DisableDateRecognition=False DisableRedirections=True
When you have the query looking just the way you want it, save the Excel web query (an icon or button for saving the web query is in the «Edit Query» window. When you save the new query, your «.iqy» file will include the options you have selected.
Other Example Web Queries
Below are a few examples of .iqy files that you might try. Websites often change, or they may change the way their APIs work, so these examples might not work forever.
Example 1: Get historical daily stock prices from Investopedia. Note that the start and end dates are text, so if you are setting up date parameters in Excel, the cells you link to need to be text rather than date values. This query uses 3 parameters: Symbol, StartDate, EndDate.
See this article for other sources for historical stock quotes.
Example 2: Get data from a published Google Spreadsheet. See my article Excel Stock Quotes in Excel to see how you can publish a Google Spreadsheet and then use a web query to get the data into Excel.
Become a Web Query Wiz
Take a few minutes to think about what mundane or repetitive tasks YOU do on the internet, particularly cases where you go to the same sites over and over to get data. If you find yourself copying and pasting or re-typing information into Excel so that you can perform calculations with the data, you may have found an ideal use for an excel web query.
There are two main things that you need in order to become a Web Query Wiz.
- A working knowledge of Excel formulas. By this, I mean the ability to use text-manipulation and other formulas. A GREAT book for both learning and reference (and the one that I use all the time) is John Walkenbach’s «Excel Formulas». If you are an avid Excel user, it will probably be the best investment you will make for a long time! The only problem I have with the book is that I need TWO copies — one for work and one for home.
- A working knowledge of HTML. This is the hard part unless you are a web designer or programmer. The articles listed in the side bar will help to some extent, but ultimately this issue was the reason why I didn’t write a more detailed tutorial. Something that will be a great help is to learn about how <form> tags work. Here is my favorite reference.
If you found this article interesting, be sure to check out some of my other Excel tips.
— Jon Wittwer, President
Vertex42, LLC
Web queries offer a handy way to import data from selected tables into a worksheet—but Excel isn’t as accommodating as it should be, especially when it comes to parameters. These tricks will let you work around Excel’s limitations so that you can get the data you need.
An Excel Web query allows you to bring data from a Web site
into an Excel worksheet. It will find any tables on the Web page and let you
select the ones containing data you want to put into your worksheet, allowing
for dynamic updates from the Web page. Web queries are not just useful for
pulling information from standard HTML pages. They can also be used quite
nicely in situations where a standard ODBC connection would be difficult or
impossible to create or maintain, such as a worksheet that’s used by
salespeople around the country.
We’re going to work through a basic example to get a feel
for how Web queries operate. Then, we’ll look at a couple of tricks that allow
you to work around some Excel limitations and see how a little VBA code can
give you the results you want. You can download this Excel workbook to see a
sample implementation of these techniques.
Getting started
To demonstrate the process, we’re going to start with a
simple Web query using Yahoo! Finance historical stock prices. This is a great
example because the data we’re interested in is presented in a plain, tabular
format. Another advantage is that the URL contains the stock symbol (GOOG, in
this case), so it will be easy to manipulate via a VBA macro, and it has little
confusing information in it. Finally, this Web query doesn’t put important
information in images or through links.
To create the Web query:
- Select
the first cell in which you want results to appear. - Choose
Data | Import External Data | New Web Query to open the dialog box shown in Figure A.
Figure A |
- Enter
the URL to query in the Address area and click the Go button (Figure B).
Figure B |
- Select
the table you want to use for the query (Figure
C).
Figure C |
- Click
the Import button.
That’s it. The data is now in your worksheet (Figure D).
Figure D |
Customizing the query
After you create a Web query, you can customize it to meet
your needs. To access Web query properties, right-click on a cell in the query
results and choose Edit Query. (You can also click Edit Query on the External
Data toolbar or choose Data | Import External Data | Edit Query.) When the Web
page you’re querying appears, click the Options button in the upper-right
corner of the window to open the dialog box shown in Figure E. The options here allow you change how the query interacts
with the Web page itself.
Figure E |
In addition, you have the same choice of Data Range
options that you have with other external data queries, such as ODBC queries. Just
right-click on a cell in your query results and choose Data Range Properties
(or click Edit Query on the External Data toolbar or choose Data | Import
External Data | Data Range Properties) to open the dialog box shown in Figure F. You’ll probably want to
change the Data Range’s name from the default to a name you
can easily access through a macro. In this example, we’ll rename the Data Range
to Stock Prices.
Figure F |
Parameters
Unfortunately, working with parameters is not as straightforward
as it could be. The wizard that created the sample query above doesn’t allow
you to put in parameters. When it fetches the Web page, it will escape the parameter
identifiers in the URL and return a page without the right results. For many
Web pages, this prevents you from selecting the table you want to use for the
import, so you’ll need to work around this Excel limitation. The Web query
parameters let you use set values, get the values from a worksheet range,
prompt the user for input, or set values programmatically via VBA macros.
One workaround is to create your query as outlined above and
then run a VBA macro to dynamically change the Connection property of the query
to provide the correct URL for Web pages that use the GET data. In this
example, we could use code similar to
Listing A
.
For Web pages that use GET
data, this is a perfectly fine solution. For Web pages that use POST data, this
is not a solution. Those scenarios will require a little bit of manual labor. You’ll
need to open a text editor and create an IQY (Internet Query) file as a plain
text file to use as the basis for your data import. The IQY file should have four
lines:
Type of Query
Query Version
URL
POST Parameters
The Query Type line should just be WEB and the Query Version
line can be whatever you want (1 is just fine). The URL line should be the URL
itself. The parameters should be in the following format:
Parameter1=Value1&Parameter2=[“Value 2”, >”Please input a value for Parameter 2: “]
This is where you can work a couple of tricks. With that
sample parameter line, “Value1” will be passed as the value for Parameter1, but
the user will be prompted with the phrase “Please input a value for Parameter
2:” to provide the value for Parameter2. This works with GET as well as POST
queries. The prompt text is optional.
In this example, we just need a single parameter–the stock
symbol. So our Web Query file looks like this:
WEB
1
http://finance.yahoo.com/q/hp
s=[“Stock Symbol”]
Once you’ve created the query file, you can use it in the
worksheet. Choose Data | Import External Data | Import Data and point the Open dialog
box to the query file you created. You’ll be prompted to specify the stock
symbol to be used. Your query results will then show up in the worksheet.
One final trick is to use this query file to fool the import
wizard into allowing you to select the exact table of desired data and still
use parameters. Once the query file has been loaded, you can edit the query in
Excel. The query won’t work because the editor won’t pass our values along
properly. It will, however, show you the Web page, and from there you can
navigate or search or whatever you need to do to see the page with the table so
you can select it. Once you’ve selected the table (just like creating a Web
query through the wizard), you can edit the parameters to use the desired methodology
(prompts, hard-coded values, or data from a worksheet range). You can also now
set these values through VBA code.
Once the Web Query has been created from the file, you no
longer need the file. It doesn’t have to be distributed with the Excel worksheet.
Working with the query through
VBA
The DataRange gets added to the QueryTables collection, which is a
member of the Worksheet object, and can be referenced by index
number or by the DataRange name. In our
example, ThisWorkbook.Sheets(“Web Query”).QueryTables(“Stock
Prices”) refers to our Web query. The most common use of VBA with
the Web Query is to have its Refresh()
method force the data to be refreshed from the source and reloaded into the worksheet.
You can also use the SetParam() method on the Parameter property of the QueryTable object to manually set (or
prompt the user to input) the value of the parameter (the Value property is read only). The sample piece of code in
Listing B
takes the stock symbol as a string, puts it into the
parameter, and then updates the query’s data.
Московская биржа предоставляет возможность скачивать котировки и другую информацию по ценным бумагам при помощи специальных ссылок.
Если мы введем такую ссылку в Power Query, создав запрос из интернета, то получим интересующие нас котировки прямо в Excel.
Contents
- Excel for Web Scraping
- Ways to Extract Data Using Excel
- How to Scrape Data from Website using Excel
- What is Excel Web Query
- Getting Started
- Advanced tactics
- Limitations with Excel
- What’s the Solution
Excel or not, web scraping is hugely important, isn’t it?
It is vital for your business whatever it may be.
Do you ever wonder how?
Well, to start with, web scraping is the process of extracting web data.
Why do you need web data?
You need web data because you base all your decisions related to business strategy on web data.
Whether it is price intelligence, sentiment analysis, or lead generation, you need data to arrive at your strategy.
With the e-commerce boom, businesses have gone online. Every moment, companies change their strategies and you need to keep a close watch on the market trends.
Guess what you need?
Data. Live. Current. Up-to-the-minute.
The question is how to get it?
Web scraping is the way out.
It can enable you to scrape web data in an automated fashion and allow you to save the same in a format of your choice. Isn’t it amazing?
It truly is.
There are various tools and techniques. But one of the great ways to scrape data in such a manner is to leverage excel for web scraping.
You might wonder how but no worries.
Here’s a complete, step-by-step tutorial to use excel to scrape data:
Excel is amazing anyway. You are no stranger to Excel. You use it for various purposes.
However, you have perhaps not yet explored how to capitalize on its potential for web scraping.
As you know, Excel is a fantastic tool to deal with data in a structured format!
Why Excel is great is because it cannot only carry out basic data computations but also perform data collection as well as analysis.
It’s quite obvious how Excel can sort, apply filter, use chart, use group and outline data, use pivot table for data analysis.
There is always a huge amount of data that needs to be scraped. It is this data that needs to be analyzed for new insights and business strategy. Where Excel comes in handy is that we need to import into Excel in order to analyze it.
When you use Excel as a web scraper tool, what it does is that it saves a lot of time and energy to scrape web data.
Here’re the ways to use it to extract web data:
1. Copy/Paste
Anyone who has used excel knows how to use the copy/past method. Anyway, it works in this way. All you need to do is to view or export the data to a new Excel, CSV or text file and then copy and paste data into the destination Excel worksheet. It should be quite easy for anyone conversant with Excel. There could be other methods more efficient for recurring use of workbooks.
2. Push
On the other hand, you can also use this method wherein you push the data from system into a specific worksheet in an existing workbook. Bear in mind that not all applications support this method. However, when they do, it will save you a step, in return some time and energy!
3. Pull
Likewise, you can pull data directly from the external data source with the help of external data feature of Excel.
This particular blog post focuses on this feature, and the relevant details related to the same.
How to Scrape Data from Website using Excel?
Excel web query is an excellent way to automate the routine task of accessing a web page and copying the data on an Excel sheet.
If you use web query, you can instruct Excel where to look (web page) and what to copy (tables of data). What this will accomplish is that Excel will automatically import the data onto a worksheet for you.
What is Excel Web Query?
It’s merely a feature in Excel. What it does is that it enables you to extract data stored on the Internet.
You would be amazed by how it can automatically find tables on the web page and let you select the particular one that you need data from.
This feature is quite useful because it creates HTML content in an Excel worksheet by passing along the necessary parameters required by the structure of the web page to display data in a workbook.
Web queries can also be fairly useful for a scenario wherein a standard ODBC (Open Database Connectivity) connect would not be easy to create or maintain for merely getting data from the web pages.
Getting Started
The best way to understand the way Excel can effectively scrape data is through an example. So we would try to retrieve Forecast data from timeanddate.com.
However, you got to remember that this sample tutorial is possible using Excel 2013 for Windows. You might not see the same kind of steps or efficiency if you use older/newer version of the program.
Step 1: Creating Excel Web Query
Step 2: Select Data
- In the New Web Query dialog box, you will be able to see a yellow box with a black arrow right at the top left of every table on the page.
- By positioning your mouse cursor over one of these arrows, the corresponding table becomes outlined in blue. If this is the table you wish to import into your spreadsheet, click on the arrow so that it changes to a green box with a checkmark and the table is highlighted in blue.
- For this example, we will select table: Forecast for the next 48 hours as shown in screenshot given below.
Step 3: Store Data to Worksheet
- Once you have completed the selection of tables to import, click on the Import button to store data into the worksheet.
- Now, Excel would display the Import Data dialog box.
- In the Import Data dialog box, instruct Excel where to place the imported web data.
- Then, select the Existing Worksheet radio button to place the table data into the existing, open, empty worksheet. Alternatively, select the New Worksheet radio button to have Excel place the table data into a newly inserted blank sheet.
- For this example, we will choose existing worksheet, and click OK button to store data in existing worksheet.
- Now, Excel would place the table data into the specified location. Sometimes, grabbing the table data might take a few moments. Excel makes an effort to grab and arrange the table information.
- Output would look somewhat like screenshot given below:
Advanced tactics
1. Customizing web query
- Once you create a Web Query, you can customize it to suit your needs. To access Web query properties, right-click on a cell in the query results and choose Edit Query.
- When the Web page you’re querying appears, click on the Options button in the upper-right corner of the window to open the dialog box shown in screenshot given below.
- The options here allow you to change how the query interacts with the Web page itself.
2. Auto refreshing Data
3. Data analysis
- Data Analysis is a process of inspecting, cleaning, transforming and modeling data with the goal of discovering useful information, suggesting conclusions and supporting decision-making
- Excel is useful because it provides commands, functions and tools that make your data analysis tasks easy. You can avoid many time consuming and/or complex calculations using Excel.
- Here you can find more about data analysis with excel.
Another example of Web Scraping using excel:
Limitations with Excel
- Not Scalable
- While Excel is great for extracting HTML tables from the web, you would do well to remember that it is not a proper web scraping tool or solution. If you wish to scrape data for business insights, Excel is not hugely helpful because it cannot extract large quantities of data. In short, you cannot scale it up as you can with a proper web scraping tool.
- Not able to scrape data that you want
- When you use Excel, you can merely extract data which is in table format.
- If you need to extract data which is not in table format, you need to find some other solution.
- Other limitations
- Moreover, you can’t get to use important features like Ajax pagination, infinite scrolling, deduplication etc. for web scraping when you use Excel for web scraping.
What’s the Solution???
- You basically need a web scraping tool or service through which you can do web scraping in a scalable fashion and provide a feature like Ajax pagination, infinite scrolling, deduplication etc.
- To sum it up, make sure you select the right kind of web scraping tool or service provider that provides good customer support and cloud-based infrastructure.
In other documents we have show you how to use the Visual Crossing Weather Data Services capability to do Query API loading of weather data directly into Excel.
There are three primary Excel use cases for Weather Data.
1) Load a fixed query set of data into a table and reference that table for further analysis.
Loading Weather Data into Excel via Web Query URL
2) A variation on the fixed query load is the 15-day forecast query which is dynamically always showing the next 15 days from “today”. So while the URL Query doesn’t change, the users will add a refresh option either manually or on load of the spreadsheet.
How to automaticall refresh weather data in Microsoft Excel
3) Dynamically pass in data to replace parameters in the API Query string based upon Excel data. Users will want to pass in locations, lists of locations, dates and other parameters. In this document we will focus on this option.
The Initial Query String
First let’s repeat what we have done in previous queries and visit the Visual Crossing Query Builder page and build a simple one-location query for Forecast data and load it into Excel. Follow along with steps from link found in number 1 above which uses the following page:
https://www.visualcrossing.com/weather/weather-data-services
Once you have a successful forecast data query, switch to the API tab to see our query string. Make sure the language drop down is set to ‘Http’ and the Output content type one to ‘CSV’. Click on the ‘Copy’ button to put the string in our system copy/paste buffer:
Next we load it into Excel by visiting the “Data” tab, selecting the “From Web” option and pasting in our URL String copied from the Visual Crossing Weather Data Query Builder above.
Click ‘OK’ and ask to Load the data which will give us the following working query:
This is the step where the other document ends but here we will begin to use parameters to customize this query.
Preparing Data for the Query
The first step is to insert some blank rows at the top of the page and enter in a list of 5 locations. This is just an area of cells with no real definition for now but will serve as our data entry area so that our users can simply update this list to get weather for these locations. To build this area simply type in the cells as seen below. Please note that we put a header “MY LOCATIONS” but this is not necessary and should not be in the data table we define. It is just for clarity.
Select the list of Cities (without the header) and create a defined table name for this area. The easiest is to Right-click on the locations selection and use the “Define Name” option. We will name our table “RAWLIST” for now.
This range is now defined and can be accessed from anywhere simply by referencing RAWLIST. However, if you prefer you can also use your own literal range definitions of cells such as “b2:b6”.
Now it is our job to make sure this list can be insert into the query so that we can retrieve multiple locations dynamically. The first step is to concatenate the list into a string that can be inserted into the URL. We want our list of city cells to be in a single string such as “Chicago, IL|Reston, VA|New York, NY”. Please note that the query has to have a list that is delimited by the pipe “|” symbol.
To do this we will use the TEXTJOIN function in Excel. (Please note that there many ways to create this same list in Excel so feel free to use your favorite technique) Click into a new cell and insert the following function.
For our example it looks like the following:
The first Parameter is the pipe symbol in quotes to define our delimiter, we put in “TRUE” as the second paramter to ignore any empty cells and we reference our table range called “RAWLIST” as the final parameter which is the list of locations.
Please note in the image above that we have a nicely joined single string ready to insert into the query. We will again right-click on our cell and give it a range name using the “Define Name” feature. We will call this range “WxLOCATIONS”.
NOTE: Previously we used the named range feature for ease of use, but for the next exercise it is required that our concatenated list has a name. We will refer to this name in the query steps.
Inserting Parameters into the Query
Lets starting by selecting our query that we made in step one, the easiest way to do this is to select it from the list of ranges as seen below or use the “Queries & Connections” window to select it.
On the right hand side of the sheet you should now see the “Queries & Connections” window. Select our query and right-click to choose to ‘Edit’ this query.
The query editor will open up as seen below and we have chosen the ‘Advanced Editor’ view:
Once opened you can see the text of query. If you look closely you will see the string that we copied from the Visual Crossing Weather Query Builder in the ‘Source’ definition. But notice that our string is only part of the query which also defines columns, types and many other pieces of metadata. We must be careful to only edit the portion we pasted in.
The first thing we need to do is to define a variable that will contain our concatenated list. To do this enter the following text above the ‘Source’ entry:
WxLoc=Excel.CurrentWorkbook(){[Name="WxLOCATIONS"]}[Content]{0}[Column1],
This new line is telling the query to define “WxLoc” as the contents of the WxLOCATIONS range from the current workbook. It is asking for the first row (row zero) of the default ‘Column1’ name. We only have one row and one column for our concatenated string anyway. Please make certain to have the comma at the end of the text to indicate the end of your variable Definition for “WxLoc”. Here is what your query should look like:
The last edit is to our original query string. Find the following text in the ‘Source’ definition:
The string says “Herndon, VA” with some escape characters, this is becauseour original query was a hardcoded query to one location at Herndon, VA. Now we need to make the locations definition dynamic. Simply replace the text after the “locations=” and before the closing parenthesis with the following:
"&WxLoc&" "
and will now look like:
locations="&WxLoc&" ")
We are basically saying that the first part of our query string + ‘OUR LOCATIONS LIST’ + last part of our query string is our new string. Effectively we are concatenating a variable into the query string by cutting out the hardcoded “Herndon, VA” string and inserting our dynamic concatenated string.
Once this is in place, the query will now dynamically grab our locations list and use it in the query to the Visual Crossing Weather Engine whenever the query is refreshed.
We can now click ‘Done’ on this query editor and the system will validate your string changes. If you get an error just double check your work and naming. Usually a misplaced quote or comma is the culprit. If successful you may see a warning from the system that looks like this:
This warning is Excel telling you that it is sending your data (list of locations) to a remote server and it needs your permission to do this. You can set the permission levels that serve your scenario best. For this exercise we will choose to Ignore the Privacy Levels, Save it and click ‘Continue’ on the warning bar.
Now we can see the results of our new query in the query preview window:
We can now verify that it is returning weather data for our list of cities. Just click ‘Close and Load’ to finish the query. Now we are back in our workbook and we can see the results of our work.
Test and Add Features
To test our work further you can update the list and then click ‘Refresh’ on the query and it will update the data live.
Please remember to set your refresh policy. Above we simply right clicked on the query results and chose ‘Refresh’. Our document here describes the common ways to refresh the query data automatically:
How to automatically refresh Weather Data in Microsoft Excel
However, this scenario may call for more user interaction such as putting in a button or dynamically detecting changes in your table range. All of which can be enabled easily using VBA objects. We found the simplest variation was to use the ‘Developer’ tab on the Ribbon Bar (may need to be added to the ribbon) and add a button then record a refresh Macro for the button to activate. There are many other enhancements here such as adding dynamic checklists of locations, hiding the concatenation string and much more, but we will leave that to the Excel expert in you to explore.