- import requests
- import pandas as pd
- from bs4 import BeautifulSoup
- # URL for the main action
- url = 'https://toolkit.tesco.com/partner/reports/'
- # Define options for each category/subcategory
- Country = ['UK', 'ROI']
- Report_type = ['Sales and stock', 'Range conformance', 'Promotional funding', 'Auto sales out retro deals', 'Fulfilment fee', 'Performance summary', 'Cost price amendments', 'Purchase order amendments']
- Product_subgroup = ['All product subgroups']
- Products = ['All products']
- View = ['TPNB – Total sales', 'TPNB – Sales x store', 'TPNB – Sales x store format', 'TPNB - Sales x DC','Product subgroup - Total sales','Stores - Total sales','Store Format - Total sales']
- Time_period = ['Last full week (Wk 08)']
- # User prompts for options
- country = input("Select Country (UK or ROI): ")
- report_type = input("Select Report Type: ")
- product_subgroup = input("Select Product Subgroup: ")
- product = input("Select Product: ")
- view = input("Select View: ")
- time_period = input("Select Time Period: ")
- # Send request to select options
- options_data = {
- 'Country': country,
- 'Report type': report_type,
- 'Product subgroup': product_subgroup,
- 'Product': product,
- 'View': view,
- 'Time period': time_period
- }
- try:
- response = requests.get(url)#, params=options_data)
- response.raise_for_status() # Raise exception for 4xx or 5xx errors
- # Parse HTML response
- soup = BeautifulSoup(response.text, 'html.parser')
- table = soup.find('table')
- # Check if table is found
- if table is None:
- raise ValueError("No tables found in the response")
- # Read HTML table into DataFrame
- df = pd.read_html(str(table))[0]
- # Construct file name based on selected options
- file_name = f"{country}_{report_type}_{product}_{view}_{time_period}.xlsx"
- # Save DataFrame to Excel file in DBFS
- dbfs_path = f"/dbfs/{file_name}"
- df.to_excel(dbfs_path, index=False)
- print(f"Report downloaded and saved to DBFS: {file_name}")
- except requests.exceptions.HTTPError as err:
- print(f"An HTTP error occurred: {err}")
- except ValueError as ve:
- print(f"ValueError: {ve}")
- except Exception as e:
- print(f"An error occurred: {e}")
[text] H
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- H
- Paste Code
- 26 Apr-2024
- 2.23 Kb
You can Share it:
Latest Code Pastes