Crawling output error

Asked 2 years ago, Updated 2 years ago, 24 views

When I simply print it out without an update, it was printed 10 times each I set it to be updated every 5 seconds, so only one article is printed.

How can I print out 10 of the same each time it is updated?

And I update it every 10 seconds for confirmation, but the program suddenly turns off while updating. How do I keep it from turning off until I enter a specific key?

import requests
from bs4 import BeautifulSoup
import time

raw = requests.get('https://search.naver.com/search.naver? 
where=news&query=%EC%86%8D%EB%B3%B4&sm=tab_srt&sort=1').text
html = BeautifulSoup(raw, 'html.parser')

articles = html.select('.type01 > li')

for article in articles:

    title = article.select_one('a._sp_each_title').text
    print(title)

    time.sleep(10)

python

2022-09-21 12:39

1 Answers

I think you wrote the wrong code.

The code you wrote is to print out 10 news stories every 10 seconds.

If you want to update every 10 seconds, please refer to the following code.

import requests
from bs4 import BeautifulSoup
import time

def update():
    # Bringing up a list of breaking news
    raw = requests.get('https://search.naver.com/search.naver?where=news&query=%EC%86%8D%EB%B3%B4&sm=tab_srt&sort=1').text
    html = BeautifulSoup(raw, 'html.parser')

    articles = html.select('.type01 > li')

    return articles

while True:
    articles = update() #Breaking news list (update)

    # Print a news list of breaking news (10 in total)
    for article in articles:
        title = article.select_one('a._sp_each_title').text
        print(title) #Output
    print()

    time.sleep(10) # Wait 10 seconds


2022-09-21 12:39

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.