• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

연애 뉴스 크롤링 8:52 부터 막힙니다

22.01.24 13:16 작성 조회수 422

2

출력을 할 때마다 계속
print(content.text)
AttributeError: 'NoneType' object has no attribute 'text'
메시지만 뜹니다
 
해결책은 계속 찾고 있습니다만
코드 입력이 문제인지 아니면 업데이트가 되서 그런지 물어보고 싶습니다
 
코드는 다음과 같습니다.
 
import requests
from bs4 import BeautifulSoup
import time

response = requests.get("https://search.naver.com/search.naver?sm=tab_sug.top&where=news&query=%EB%B8%94%EB%9E%99%ED%95%91%ED%81%AC&oquery=%EC%86%90%ED%9D%A5%EB%AF%BC&tqi=hPGDksp0Yidss4HuFwdssssstKV-439002&acq=%EB%B8%94%EB%9E%99&acr=1&qdt=0")
html = response.text
soup = BeautifulSoup(html, 'html.parser')
articles = soup.select("div.info_group")
for article in articles:
    links = article.select("a.info")
    if len(links) >=2:
        url = links[1].attrs['href']
        response = requests.get(url, headers={'User-agent':'Mozila/5.0'})
        html = response.text
        soup = BeautifulSoup(html, 'html.parser')

        # if calls ent-news
        if "entertain" in response.url:
            content = soup.select_one("#articleBody")
        else:
            content = soup.select_one("#articleBodyContents")
        print(content.text)
        time.sleep(0.3)
 

답변 3

·

답변을 작성해보세요.

1

jangtaesan님의 프로필

jangtaesan

2022.07.16

안녕하세요 : )

class101에서 강의 잘 듣고 다시 여기로 넘어왔습니다.

듣고 싶은게 더 있어서요. 복습도 되고 넘 좋습니다.

연예 뉴스 크롤링에서 네이버의 변경이 있었네요. 

entertain으로 시작하는 주소는 같은데

news.naver.com 부분이
n.news.naver.com으로 변경이 되면서
id값과 class 값이 변경되었습니다.

수정코드(2022년 07월 16일 시점) 올려봅니다.

if "entertain" in response.url:
  title=soup.select_one(".end_tit")
  content=soup.select_one("#articeBody")
else:
  title=soup.select_one(".media_end_head_headline

")
  content=soup.select_one("#dic_area")

정보 공유 감사합니다!!!!

Jaehyun Lee님의 프로필

Jaehyun Lee

2022.11.13

감사합니다. 11월 13일 현재 잘 됩니다.

1

오타입니다....!!

content = soup.select_one("#articleBody")

여기 잘 찾아 보세요 :)

다시 확인해보니까 l이 빠지네요

감사합니다

0

새벽별님의 프로필

새벽별

2023.02.20

2023.02.20.월 현제 #articeBody가 맞네요.

저는 아이디와 클래스이름이 바뀌어서

다른 것으로 했는데요. 클래스를 .클래스명으로 해야 하는데, #클래스명으로해서 결과가

'NoneType' object has no attribute 'text'

이렇게 나와서 반나절을 왜 안되나 하고 원인을 찾았습니다.

클래스는 .

아이디는 #

이것부터 확인해야 겠습니다.