Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Web.Contents: How to Pull Web Data

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Power Query's Web.Contents function retrieves data directly from web pages, APIs, or online services, turning URLs into structured tables in Excel or Power BI. It works by sending an HTTP request to a specified endpoint and parsing the response, commonly used for JSON, XML, or HTML data. Before using it, you need the exact URL of the data source and must understand its structure — no authentication or special tools are required beyond Excel or Power BI with Power Query enabled.

What You'll Need

  • Microsoft Excel 2016 or later, or Power BI Desktop (both include Power Query)
  • The full, correct URL of the web API or data endpoint you want to pull from
  • Basic understanding of whether the source returns JSON, XML, or HTML (helpful but not required)
  • Internet connection to reach the web service

Step-by-Step

  1. Open Excel or Power BI Desktop and go to the Data tab. Click Get Data, then select From Other Sources, then From Web.
  2. In the dialog box that appears, paste the full URL of your data source into the URL field. Ensure the URL includes https:// or http:// and no trailing spaces.
  3. Click OK. Power Query will attempt to connect to the server. If the site requires authentication, you may be prompted to choose a method — select Anonymous unless you know credentials are required.
  4. After the connection, a navigator window may appear if the response is structured (e.g., JSON with multiple objects). If you see a table, select it; if you see raw text, click Transform Data to open the Power Query Editor.
  5. In the Power Query Editor, look at the Content column. If it contains text (like JSON), click the icon at the top of that column and choose JSON from the list. If the data is XML, choose XML. For HTML, choose HTML Table if you want tabular output.
  6. If the JSON or XML is nested, expand it by clicking the double-chevron icon (→) next to column headers. Select the fields you need — uncheck others to reduce clutter.
  7. Use the Transform tab to promote headers if needed, change data types (e.g., text to number), or split columns by delimiters.
  8. Click Close & Load to load the cleaned data into your worksheet or report. The query will refresh automatically when you reopen the file or click Refresh All.

Common Mistakes

  • Using an incomplete URL — missing https://, trailing slashes, or typos will cause connection failures.
  • Assuming web pages (like blogs or news sites) return structured data — Web.Contents only works with APIs or endpoints that output machine-readable formats like JSON/XML, not rendered HTML pages.
  • Not handling authentication — some APIs require API keys or tokens. If you skip this, the server returns a 401 or 403 error, and Power Query will fail silently unless you check the error details.
  • Trying to parse HTML as JSON — if the endpoint returns a webpage instead of an API response, selecting JSON will throw an error because the text isn’t valid JSON syntax.
  • Forgetting to expand nested fields — a common JSON response like {"data": [{"id":1,"name":"John"}]} appears as a single column. You must expand the data field to access the records inside.

Tips

  • Test your URL in a browser first — if you see raw JSON or XML, it’s likely compatible. If you see a webpage, it probably won’t work unless you can find the underlying API endpoint.
  • Use Web.Contents() in Advanced Editor to see the M code — this helps you understand how parameters are passed and allows manual edits for headers or query strings.
  • For APIs requiring headers (like Authorization), click Advanced in the Web dialog and add custom headers. For example, add Authorization with value Bearer yourtokenhere.
  • Use Table.AddColumn with Web.Contents in M code if you need to loop through multiple URLs dynamically — useful for batch requests.
  • Always check the Query Settings pane on the right — it shows the exact URL and any applied parameters, helping you debug later.

Troubleshooting

  • Connection failed: Check your internet, then try the URL in a browser. If the browser shows a 404 or 403 error, the endpoint doesn’t exist or requires authentication. Contact the data provider for correct access details.
  • Invalid JSON error: The content isn’t valid JSON. Go back to the navigator and check if you selected the correct format. If the source returns HTML, you cannot parse it as JSON — you need to find the real API.
  • Timeout error: The server is slow or the request is too large. In Power Query Editor, go to Query Settings > Advanced Editor and add [Timeout=#duration(0,0,5,0)] to extend the timeout. Default is 30 seconds.
  • Data doesn’t refresh: Some servers block automated requests. Check if the site has a robots.txt file or terms of service restricting scraping. Use headers like User-Agent to mimic a browser if allowed.
  • Authentication required: If you’re prompted for credentials, try Windows, Basic, or API Key options. If unsure, consult the API documentation — many services provide keys in their developer portal.

Frequently Asked Questions

Can I use Web.Contents with any website?

No. It only works with endpoints that return structured data (JSON, XML, or HTML tables). Standard web pages (like news sites or blogs) return HTML meant for browsers, not machines, and will not parse correctly.

Do I need an API key to use Web.Contents?

Only if the service requires it. Many public APIs do, like weather or financial services. If the service asks for a key, you must obtain one from their developer portal and include it in the URL or as a custom header.

Why does my data disappear after refreshing?

This happens if the server returns a different structure (e.g., empty array or error message). Check the query’s error log in Power Query Editor after refresh. You may need to add error handling with try ... otherwise in M code.

Can I pull data from sites that require login?

Not directly. Web.Contents cannot maintain login sessions. You must use API keys, cookies, or tokens obtained outside Power Query. For login-based sites, consider using browser automation tools instead.

Is Web.Contents secure?

Yes, as long as you use HTTPS URLs. Power Query encrypts data in transit. Never use HTTP for sensitive data. Avoid pasting credentials into URLs — use headers or authentication options instead.

How do I update the URL after the query is created?

Go to the Query Settings pane on the right, click the gear icon next to the source step, and edit the URL. Alternatively, open the Advanced Editor and change the string inside Web.Contents("...").