Skip to content
Advertisement

My Python Output Repeats Itself Hundreds of Time

I am learning to scrape data from a website. I have a problem when printing my code using jupyter notebook. My output repeats itself many times and I don’t know how to fix it. Here’s my trial code:

JavaScript

I thought it was fine until I print it out:

JavaScript

And hundreds more….

All answers are so much appreciated. Thanks!

Advertisement

Answer

The for data in datas loop is causing your issue. There is no issue with your regex, as some of the answers have suggested Let’s take a look at your code’s execution.

I’ve corrected the code at the bottom of the post, but first I want to talk you through the issue.

When you create the variable data, you join multiple things into a single string. As a demonstration, you should run print(type(datas)). What this means, is that your data has already been put together.

When you loop through the string, you actually end up looping through each letter of that string. As a test, try running this:

JavaScript

The program will output each letter on a new line, like so:

JavaScript

The Solution All you need to do is remove that for loop and everything will work fine. Here is what your final code should look like:

JavaScript

This should give you the result:

JavaScript
Advertisement