강의

멘토링

커뮤니티

Inflearn Community Q&A

letskirill's profile image
letskirill

asked

[New Revised Edition] This is Real Web Crawling - Practical Edition (AI Monetization)

Select by attribute

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

Written on

·

625

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)
 
웹-크롤링python

Answer 3

1

안녕하세요 : )

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")

startcoding님의 프로필 이미지
startcoding
Instructor

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

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

1

startcoding님의 프로필 이미지
startcoding
Instructor

오타입니다....!!

content = soup.select_one("#articleBody")

여기 잘 찾아 보세요 :)

letskirill님의 프로필 이미지
letskirill
Questioner

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

감사합니다

0

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

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

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

'NoneType' object has no attribute 'text'

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

클래스는 .

아이디는 #

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

letskirill's profile image
letskirill

asked

Ask a question