• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    해결됨

03. 연예뉴스크롤링하기에 질문 있습니다.

23.04.03 10:09 작성 조회수 247

0

import requests
from bs4 import BeautifulSoup
import time

response = requests.get(
    "https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%EB%B8%94%EB%9E%99%ED%95%91%ED%81%AC")
html = response.text
soup = BeautifulSoup(html, 'html.parser')
articles = soup.select("div.info_group")  # 뉴스 기사 div 10개 추출
for article in articles:
    links = article.select("a.info")  # 리스트
    if len(links) >= 2:  # 링크가 2개 이상이면
        url = links[1].attrs['href']  # 두번째 링크의 href를 추출
        response = requests.get(url, headers={'User-agent':'Mozila/5.0'})
        html = response.text
        soup = BeautifulSoup(html, 'html.parser')

        # 만약 연예 뉴스라면
        if "entertain" in response.url:
            title = soup.select_one(".end_tit")
            content = soup.select_one("#articleBody")
        else:
            title = soup.select_one(".media_end_head_headline")
            content = soup.select_one("#dic_area")
        print("====링크====", url)
        print("====제목====", title.text.strip())
        print("====본문====", content.text.strip())
        time.sleep(0.3)

위와 같이 코드를 짜고 실행했는데,

https://entertain.naver.com/read?oid=629&aid=0000209267

이사이트 차례가 되자 content를 못 뽑아냅니다.

다음과 같은 에러를 나타냅니다.

====링크==== https://n.news.naver.com/mnews/article/629/0000209267?sid=106
====제목==== 블랙핑크 지수, 요란하지 않은 '꽃'의 유혹[TF초점]
Traceback (most recent call last):
  File "C:\Users\asd20\Desktop\startcoding_crawling\Chapter04\03.연예뉴스크롤링하기.py", line 27, in <module>
    print("====본문====", content.text.strip())
AttributeError: 'NoneType' object has no attribute 'text'

개발자도구를 눌러 html 구조를 확인해봐도 원인을 잘 모르겠습니다.

답변 2

·

답변을 작성해보세요.

0

오타 나기 쉬운 부분입니다ㅋㅋ

0

asd203n님의 프로필

asd203n

질문자

2023.04.03

"#articleBody"가 아니라

"#articeBody"에요.