Z
Z
Zonder_Play2021-07-22 00:35:08
Python
Zonder_Play, 2021-07-22 00:35:08

What to do if the program displays the list 2 times when parsing in Python?

I wrote the code according to the guide. When run, it displays the list 2 times, but it needs 1.
Here is my code:

import requests
from bs4 import BeautifulSoup
def parse():
    URL = 'https://my.ranepa.ru/pk/list.php?FT=1&FL=2&FK=&FC=&FB=&FO=&F1=d28d6cfc-36b6-11e6-80ca-005056a02ba9&F2=4ecdef8b-7658-11e9-80f4-005056a03e4f&F3=6e9ce087-c78b-4932-88a5-2628115de4b3&F4=f236a8f4-8f5c-11e9-80f8-005056a03e4f'
    HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.164 Safari/537.36'
        }
    response = requests.get(URL, headers = HEADERS)
    soup = BeautifulSoup(response.content, "html.parser")
    items = soup.findAll('div', class_ = 'bigtable_wrap')
    components = []

    for item in items:
        components.append({
            'title':item.find('tbody').get_text(strip = True)
        })
        for component in components:
            print(component['title'])
parse()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wispik, 2021-07-22
@Wispik

You have the second for loop nested in the first, so the information is displayed several times. Take the second out of the first (write them on the same level)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question