[text] H

Viewer

  1. import requests
  2. import pandas as pd
  3. from bs4 import BeautifulSoup
  4.  
  5. # URL for the main action
  6. url = 'https://toolkit.tesco.com/partner/reports/'
  7.  
  8. # Define options for each category/subcategory
  9. Country = ['UK', 'ROI']
  10. Report_type = ['Sales and stock', 'Range conformance', 'Promotional funding', 'Auto sales out retro deals', 'Fulfilment fee', 'Performance summary', 'Cost price amendments', 'Purchase order amendments']
  11. Product_subgroup = ['All product subgroups']
  12. Products = ['All products']
  13. 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']
  14. Time_period = ['Last full week (Wk 08)']
  15.  
  16. # User prompts for options
  17. country = input("Select Country (UK or ROI): ")
  18. report_type = input("Select Report Type: ")
  19. product_subgroup = input("Select Product Subgroup: ")
  20. product = input("Select Product: ")
  21. view = input("Select View: ")
  22. time_period = input("Select Time Period: ")
  23.  
  24. # Send request to select options
  25. options_data = {
  26.     'Country': country,
  27.     'Report type': report_type,
  28.     'Product subgroup': product_subgroup,
  29.     'Product': product,
  30.     'View': view,
  31.     'Time period': time_period
  32. }
  33.  
  34. try:
  35.     response = requests.get(url)#, params=options_data)
  36.     response.raise_for_status()  # Raise exception for 4xx or 5xx errors
  37.     
  38.     # Parse HTML response
  39.     soup = BeautifulSoup(response.text, 'html.parser')
  40.     table = soup.find('table')
  41.     
  42.     # Check if table is found
  43.     if table is None:
  44.         raise ValueError("No tables found in the response")
  45.     
  46.     # Read HTML table into DataFrame
  47.     df = pd.read_html(str(table))[0]
  48.     
  49.     # Construct file name based on selected options
  50.     file_name = f"{country}_{report_type}_{product}_{view}_{time_period}.xlsx"
  51.     
  52.     # Save DataFrame to Excel file in DBFS
  53.     dbfs_path = f"/dbfs/{file_name}"
  54.     df.to_excel(dbfs_path, index=False)
  55.     
  56.     print(f"Report downloaded and saved to DBFS: {file_name}")
  57.     
  58. except requests.exceptions.HTTPError as err:
  59.     print(f"An HTTP error occurred: {err}")
  60. except ValueError as ve:
  61.     print(f"ValueError: {ve}")
  62. except Exception as e:
  63.     print(f"An error occurred: {e}")

Editor

You can edit this paste and save as new: