[text] G

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. # Function to prompt user for option selection
  17. def prompt_user(options, category_name):
  18.     print(f"Select {category_name}:")
  19.     for idx, option in enumerate(options, start=1):
  20.         print(f"{idx}. {option}")
  21.     while True:
  22.         choice = input(f"Enter the number corresponding to your choice (1-{len(options)}): ")
  23.         if choice.isdigit() and 1 <= int(choice) <= len(options):
  24.             return options[int(choice) - 1]
  25.         else:
  26.             print("Invalid input. Please enter a number within the specified range.")
  27.  
  28. # User prompts for options
  29. country = prompt_user(Country, "Country")
  30. report_type = prompt_user(Report_type, "Report Type")
  31. product_subgroup = prompt_user(Product_subgroup, "Product Subgroup")
  32. product = prompt_user(Products, "Product")
  33. view = prompt_user(View, "View")
  34. time_period = prompt_user(Time_period, "Time Period")
  35.  
  36. # Send request to select options
  37. options_data = {
  38.     'Country': country,
  39.     'Report type': report_type,
  40.     'Product subgroup': product_subgroup,
  41.     'Product': product,
  42.     'View': view,
  43.     'Time period': time_period
  44. }
  45.  
  46. try:
  47.     response = requests.get(url)#, params=options_data)
  48.     response.raise_for_status()  # Raise exception for 4xx or 5xx errors
  49.     
  50.     # Parse HTML response
  51.     soup = BeautifulSoup(response.text, 'html.parser')
  52.     table = soup.find('table')
  53.     
  54.     # Check if table is found
  55.     if table is None:
  56.         raise ValueError("No tables found in the response")
  57.     
  58.     # Read HTML table into DataFrame
  59.     df = pd.read_html(str(table))[0]
  60.     
  61.     # Construct file name based on selected options
  62.     file_name = f"{country}_{report_type}_{product}_{view}_{time_period}.xlsx"
  63.     
  64.     # Save DataFrame to Excel file in DBFS
  65.     dbfs_path = f"/dbfs/{file_name}"
  66.     df.to_excel(dbfs_path, index=False)
  67.     
  68.     print(f"Report downloaded and saved to DBFS: {file_name}")
  69.     
  70. except requests.exceptions.HTTPError as err:
  71.     print(f"An HTTP error occurred: {err}")
  72. except ValueError as ve:
  73.     print(f"ValueError: {ve}")
  74. except Exception as e:
  75.     print(f"An error occurred: {e}")

Editor

You can edit this paste and save as new:


File Description
  • G
  • Paste Code
  • 26 Apr-2024
  • 2.8 Kb
You can Share it: