DEV Community

David García
David García

Posted on

Python + AI: 6 automations any freelancer can ship this week

```html

Let’s be honest. As freelancers, our time is gold. We’re juggling client requests, chasing invoices, and desperately trying to find a few extra hours to actually build something awesome. The thought of diving into complex AI projects can be intimidating, but the truth is, you can leverage AI for immediate automation with just a little Python. This isn't about building a full-blown chatbot; it's about tackling small, impactful tasks that free up your brainpower.

The Problem: Repetitive Tasks Suck

We've all been there. Spending an hour manually formatting a CSV file, renaming a batch of images, or extracting data from a website. These tasks are essential, but they’re also incredibly time-consuming and frankly, a massive drain on your productivity. As a freelancer, you’re a business. Those hours are costing you money. Trying to build custom scripts from scratch for each of these tasks is overkill – you need something quick and easy.

Solution: Simple Data Extraction with Beautiful Soup and OpenAI

Let’s look at a practical example: automatically extracting the price of a product from an e-commerce website using Python and OpenAI’s API. This is a common request from clients, and automating it is a huge win.


import requests

from bs4 import BeautifulSoup

import openai

openai.api_key = "YOUR_OPENAI_API_KEY" Replace with your actual API key

def get_product_price(url):

try:

response = requests.get(url)

response.raise_for_status() Raise HTTPError for bad responses (4xx or 5xx)

soup = BeautifulSoup(response.content, 'html.parser')

price_element = soup.find('span', class_='product-price') Adjust selector as needed

if price_element:

price_text = price_element.text.strip()

prompt = f"Extract the numerical price from the following text: {price_text}"

completion = openai.Completion.create(

engine="text-davinci-003",

prompt=prompt,

max_tokens=10

)

price = completion.choices[0].text.strip()

return float(price)

else:

return None

except requests.exceptions.RequestException as e:

print(f"Error fetching URL: {e}")

return None

Example usage:

url = "https://www.example.com/product/123" Replace with the product URL

price = get_product_price(url)

if price:

print(f"Product Price: {price}")

else:

print("Price not found.")

Explanation: This code uses the `requests` library to fetch the webpage content, `BeautifulSoup` to parse the HTML, and the OpenAI API to extract the numerical value from the text. The `openai.Completion.create()` function sends a prompt to OpenAI, asking it to extract the price. The `response.raise_for_status()` line is crucial for handling potential errors during the HTTP request. We then convert the extracted string to a float.

Practical Results

With this simple script, you can automate the extraction of product prices from various e-commerce sites. You'd need to adjust the CSS selector (`'span', class_='product-price'`) to match the specific website's HTML structure, but the core logic remains the same. This kind of automation can save you hours per week.

Conclusion & Next Steps

You don’t need to be a machine learning expert to boost your productivity as a freelancer. AI, when used strategically, can be a powerful tool for automating those tedious, repetitive tasks. Want a deeper dive into how AI can optimize your workflow and identify areas for automation within your specific business? Schedule a free consultation today to discuss your needs and how I can help you build a more efficient and profitable operation.

```


Itelnet Consulting

Top comments (0)