Coding with ChatGPT

8 ways to use ChatGPT for coding. From troubleshooting to data extraction.

Content

Hi, we’re Apify, a full-stack web scraping and browser automation platform. A big part of what we do is getting better data for AI. Check us out!

Should you use ChatGPT for coding?

There's a big problem with large language models: no one really knows what they're capable of.

This doesn't stop people from using them, but it can be tricky to identify the right applications and utilize them effectively for those applications.

There's an invisible line, which has been referred to as a jagged technological frontier. On the one side are the things LLMs can do, and on the other, the things they can't.

Many of us have been surprised to learn that generative AI struggles with some things we expected it to manage, like basic math. However, it can sometimes manage things we wouldn't think of delegating to a machine, like creative ideas.

On which side of this invisible line is coding?

Specifically, can you use ChatGPT - the most famous of LLMs - to do it?

I'm going to answer that with my own experience of coding with ChatGPT.

Bear in mind I'm not a developer.

Still, as someone who writes web scraping tutorials, I've found that coding has been the most beneficial application of ChatGPT so far.

How I use ChatGPT for coding

1. Data visualization

Let's kick off with the biggest surprise.

I asked ChatGPT to show me how to visualize a Pandas DataFrame with Matplotlib:

Coding_with_ChatGPT_-_turning_a_Pandas_DataFrame_into_a_plot_with_Matplotlib.jpeg

It began much as I'd expected, but it then proceeded to try to parse the data.

Huh? I'd never seen it attempt that before!

Coding_with_ChatGPT_-_parsing_data.jpeg

It was struggling, as I would have expected it to. But it was curious that it was trying to run the code rather than give me the code to run myself.

I decided to give it a helping hand.

I inserted the code I had created for a previous tutorial:

Would it be easier to do it with this?

yahoo = pd.read_html ("https://finance.yahoo.com/most-active/")
yahoo
Coding_with_ChatGPT_-_Pandas_and_Matplotlib.jpeg

Now it was behaving as I'd expected: it just gave me the code.

But ChatGPT sometimes has confidence issues. So, I decided to give it some gentle encouragement:

Coding_with_ChatGPT_-_running_code_and_producing_an_output.jpeg

Note: ChatGPT often responds well to positive feedback

Coding_with_ChatGPT_-_data_visualization.jpeg

I was flabbergasted. I had no idea ChatGPT could run code and produce an output!

Curious to see if it could do this for anything other than HTML tables scraped with Pandas, I gave it something I thought might be a bit trickier.

Which brings me to use #2:

2. Adapting and running code

I borrowed some code from an article on Python dictionaries that I thought would be appropriate for visualization and fed it to my self-doubting AI assistant:

Coding_with_ChatGPT_-_Python_dictionary.jpeg

As you can see, it proceeded to show me how to plot the data but was still shy about running the code. So, I had to encourage it again:

Coding_with_ChatGPT_-_Python_data_visualization.jpg

Wow! What else could it do? I wondered.

3. Uploading files for data analysis

How about giving it a dataset and asking it to visualize what it contains?

Coding_with_ChatGPT_-_uploading_a_CSV_file_for_analysis_and_visualization.jpeg

So far, so good…

Coding_with_ChatGPT_-_creating_a_bar_chart_from_a_dataset.jpeg

Well, I'll be damned!

4. Explaining code

When you've seen as much AI-generated content as I have in my role as editor at Apify, it's easy to spot stuff that ChatGPT vomited out.

However, for AI explanations of code, I think I need more time working with it to recognize all the patterns.

One thing I have noticed already, though, is that ChatGPT often mansplains and tells you the blindingly obvious.

Not good when you're writing for people who know how to code.

Yet, for me, a mere initiate in Python coding, explanations of the obvious do a lot of good.

So, I put ChatGPT to the test on what you saw above:

Coding_with_ChatGPT_-_Explaining_code.jpeg
Coding_with_ChatGPT_-_explaining_output_with_code.jpeg

This made me think, Do I even need to run the code myself?

The whole reason I was doing this was to get the code, run it in my terminal, and adapt it for the application I had in mind until I got the result I wanted.

Maybe it was ChatGPT's lack of confidence, but when I asked, it had this to say:

Coding_with_ChatGPT_vs_Python_libraries.jpeg

It went on to list more than what you see above, but you get the idea.

5. Generating sample code

Let's move on from data visualization and look into a more general use case.

One of ChatGPT's most common applications is generating code. There are things to watch out for when using it for this purpose.

The code it will provide will be generic and not functional. That should be obvious. But it does mean that if you want to demonstrate something like building a web scraper, for example, you're going to have problems.

Yes, you can adjust the code or ask the AI to adapt it, but do you know whether you're using the right tool for the data in question?

Have you selected the right elements? Is the web data behind a 'See More' button? Will you have to deal with dynamically loaded content or CAPTCHAs?

Generic code will only get you so far.

If you're a developer, AI might save you some time because you presumably know what you're doing.

If not, AI might not save you much time. But it can be a skills-leveler and enable you to do things you couldn't manage without it.

Either way, you really ought to inspect the data or website in question and determine which tools you should use. This will save you a lot of back-and-forth with AI.

Having said that, here's a good example of using ChatGPT to generate sample code.

I'd written an article on using MechanicalSoup for web scraping.

As is customary at Apify, the draft was code-checked by a developer. The reviewer suggested that before discussing the library's features (navigation, form handling, session management), I should start with a basic code example—“6 lines or so”—of MechanicalSoup.

I used that feedback to prompt ChatGPT, and it gave me exactly what I needed in this instance:

Coding_with_ChatGPT_-_sample_code_for_MechanicalSoup.jpeg

Note: Even if AI-generated code functions, it doesn't mean it's following best practices. If you're not a developer, you should always check the code against documentation and the advice of developers in places like Discord and StackOverflow.

6. Troubleshooting and data cleaning

As with any other use of AI, you should NEVER EVER trust the code. Always run it yourself.

People who know how to code better than I (that's probably everyone reading this article) won't need to spend as much time debugging and adjusting code as I do. But ChatGPT's ability to fix code was a tipping point for me.

I find what works best is screenshots. This has been possible with ChatGPT-4 since it became multi-modal.

You can copy and paste code and error messages, but it seems that ChatGPT-4 and ChatGPT-4o handle images better. Strange for tools we call language models, right?

Here's a really basic example of using ChatGPT for fixing code.

While experimenting (I changed this code later), the first thing I noticed was the enormous spacing between lines in the output.

So, I asked ChatGPT to adapt the code to improve it:

Coding_with_ChatGPT_-_data_cleaning.jpg

This made matters worse:

Coding_with_ChatGPT_-_adapting_code.jpg

It didn't go perfectly after one attempt, but it didn't take long at all to get the result I wanted:

Data cleaning with ChatGPT output

7. Web scraping with ChatGPT

I've already touched upon the limitations of using AI for web scraping. But that's not to say that it's of no use for scraping tasks.

One of the most effective applications of ChatGPT for web scraping isn't the data collection but the formatting of the extracted data.

Here's an example from an article I wrote a while back on AI web scraping:

Say you've built a scraper that collects text from a page. You can feed the data to ChatGPT and tell it to make a JSON out of it.

Here's an example prompt:

Here follows body.innerText for a blog listing page. Extract article names, descriptions, author names, and creation dates, and format those as a JSON array…

You can see the output in the article I mentioned earlier.

What's the benefit of this?

Using AI to extract information from the scraped data and format it is more resilient to page changes than regular scraping approaches. That's because this approach doesn’t involve CSS selectors, which can stop working after a change of design or page layout.

If you decide to use this approach, remember to be as specific as possible and always describe the schema with attribute names (e.g., “title”, “author_name”, “publication_date”).

8. Adding scraping functionality to custom GPTs

Since OpenAI introduced custom GPTs, it's been possible to build your own GPT and add web scraping capabilities to it.

The most straightforward way to do this is with microapps known as Apify Actors.

These Actors (effectively Dockers under the hood) are web scraping and automation tools that can automatically generate the OpenAPI specification for their API. This makes it extremely easy to call them from your GPTs.

If you're interested in this use of Actors, this step-by-step guide to web scraping with ChatGPT will show you what to do.

And that's not all.

You can also add scraped data to a GPT's knowledge. To do this, follow my guide to adding a knowledge base to your GPTs by scraping and uploading data.

Conclusion: How to use ChatGPT for coding

What should you take away from all I've said about coding with ChatGPT?

Here are 4 key points to remember:

  • #1. Use ChatGPT as a coding tutor

If you're a coding beginner, like myself, ChatGPT is a pretty good teacher. It will give you code samples, tell you where you're going wrong, adapt your code, and (in some cases) even run code and produce an output.

  • #2. Upload files and screenshots

Don't rely on text alone when prompting ChatGPT. AI can work with uploaded data files and screenshots to help you produce the output you want.

  • #3. Run the code

Always run AI-generated code in your code editor. Download and install whatever libraries you think you'll need for your particular case. And you can ask ChatGPT to help you with that, too!

  • #4. Give feedback

Let the LLM know when something has worked, or something hasn't. It tends to respond well to feedback and encouragement. Weird, but true.


With that, I leave you to try out these experiments for yourself.

They worked for me. I hope you have the same luck!

Theo Vasilis
Theo Vasilis
Writer, Python dabbler, and crafter of web scraping tutorials. Loves to inform, inspire, and illuminate. Interested in human and machine learning alike.

Get started now

Step up your web scraping and automation