Enter multiple Naver article crawling search terms

Asked 2 years ago, Updated 2 years ago, 48 views

I want to print out both the news search results that I searched for in breaking news and the news search results that I searched for in 1 step, but how can I modify them?

I'm using the method below, but it looks messy.

Thank you Iluno for your quick and kind answer, but I wonder how to modify only the search_word part without modifying the large frame if possible!!

import requests
from bs4 import BeautifulSoup

search_word = 'Breaking News'
url = f'https://search.naver.com/search.naver?where=news&query= 
{search_word}&sm=tab_srt&sort=1'

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
news_titles = soup.select('.news .type01 li dt a[title]')

print()
for title in news_titles:
    print(title['title'])


search_word = "One Step"
url = f'https://search.naver.com/search.naver?where=news&query= 
{search_word}&sm=tab_srt&sort=1'

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
news_titles = soup.select('.news .type01 li dt a[title]')

print()
for title in news_titles:
    print(title['title'])

python crawling naver

2022-09-21 12:39

1 Answers

The process of retrieving and printing news results is simplified by representing them as functions.

import requests
from bs4 import BeautifulSoup

def articles(search_word):
    # Functions that search and print news
    url = 'https://search.naver.com/search.naver?where=news&query=%s&sm=tab_srt&sort=1' % search_word
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    news_titles = soup.select('.news .type01 li dt a[title]')

    print()
    for title in news_titles:
        print(title['title'])

articles ('breaking news') # breaking news output
articles ('1 step') # 1 step output


2022-09-21 12:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.