get paid to paste

TL;DW Tutorials - Python Junk Food/Food Recall...

import requests
import pandas as pd

# Function to get recall data from the FDA API
def get_recall_data(product):
    base_url = 'https://api.fda.gov/food/enforcement.json'
    params = {
        'search': f'product_description:"{product}"',
        'limit': 100
    }
    response = requests.get(base_url, params=params)
    if response.status_code == 200:
        data = response.json()
        if 'results' in data:
            return [
                {
                    'Product': product,
                    'Recall Date': result.get('recall_initiation_date', 'N/A'),
                    'Reason': result.get('reason_for_recall', 'N/A')
                }
                for result in data['results']
            ]
        else:
            print(f"No results found for product {product}")
            return []
    else:
        print(f"Error: {response.status_code} for product {product}")
        return []

# List of products to search for
products = ["Doritos", "Cheetos", "Lay's", "Pringles", "Ritz", "Oreo", "Snickers"]

# Initialize an empty list to store results
all_results = []

# Loop through each product and get recall data
for product in products:
    all_results.extend(get_recall_data(product))

# Convert the results to a DataFrame
df = pd.DataFrame(all_results)

# Save the DataFrame to a CSV file
df.to_csv('popular_snacks_recall_data.csv', index=False)
print("Data has been exported to popular_snacks_recall_data.csv")

Pasted: May 28, 2024, 10:44:43 am
Views: 131