[text] F

Viewer

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import pandas as pd
  4. import os
  5.  
  6. # URL for the main action
  7. url = 'https://toolkit.tesco.com/partner/reports/'
  8.  
  9. # Define options for each category/subcategory
  10. Country = ['UK', 'ROI']
  11. Report_type = ['Sales and stock', 'Range conformance', 'Promotional funding', 'Auto sales out retro deals', 'Fulfilment fee', 'Performance summary', 'Cost price amendments', 'Purchase order amendments']
  12. Product_subgroup = ['All product subgroups']
  13. Products = ['All products']
  14. 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']
  15. Time_period = ['Last full week (Wk 08)']
  16.  
  17. # Function to save Excel file to DBFS workspace
  18. def save_to_dbfs_workspace(df, file_name):
  19.     dbfs_path = "/dbfs/mnt/workspace/"  # Assuming workspace is mounted at '/mnt/workspace'
  20.     file_path = os.path.join(dbfs_path, file_name)
  21.     df.to_excel(file_path, index=False)
  22.     print(f"Report downloaded and saved to DBFS workspace: {file_name}")
  23.  
  24. # Iterate over combinations of options
  25. for country in Country:
  26.     for report_type in Report_type:
  27.         for product_subgroup in Product_subgroup:
  28.             for product in Products:
  29.                 for view in View:
  30.                     for time_period in Time_period:
  31.                         # Send request to select options
  32.                         options_data = {
  33.                             'Country': country,
  34.                             'Report type': report_type,
  35.                             'Product subgroup': product_subgroup,
  36.                             'Product': product,
  37.                             'View': view,
  38.                             'Time period': time_period
  39.                         }
  40.                         try:
  41.                             response = requests.get(url)#, params=options_data)
  42.                             response.raise_for_status()  # Raise exception for 4xx or 5xx errors
  43.                             # Process the response as needed
  44.                             print(f"Options selected: {options_data}")
  45.                             # Extract data from HTML response
  46.                             soup = BeautifulSoup(response.text, 'html.parser')
  47.                             table = soup.find('table')
  48.                             df = pd.read_html(str(table))[0]
  49.                             # Construct file name based on selected options
  50.                             file_name = f"{country}_{report_type}_{product}_{view}_{time_period}.xlsx"
  51.                             # Save DataFrame to DBFS workspace
  52.                             save_to_dbfs_workspace(df, file_name)
  53.                         except requests.exceptions.HTTPError as err:
  54.                             print(f"An error occurred: {err}")

Editor

You can edit this paste and save as new:


File Description
  • F
  • Paste Code
  • 26 Apr-2024
  • 2.82 Kb
You can Share it: