인프런 커뮤니티 질문&답변
워드 클라우드 관련 문의 드립니다.
작성
·
195
1
- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.
강의 되로 입력 후 실행 하였는데
AttributeError: AttributeError: 'NoneType' object has no attribute 'text 가 뜹니다.
도움 요청 드립니다.
=============================================================
import requests
from bs4 import BeautifulSoup
import time
import pyautogui
import pyperclip
# 사용자입력
keyword = pyautogui.prompt("검색어를 입력하세요")
lastpage = int(pyautogui.prompt("몇 페이지까지 크롤링 할까요?"))
# 본문 전체 내용
total_content = ""
# 기사 개수
article_num = 0
page_num = 1
for i in range(1, lastpage * 10, 10):
print(f"{page_num}페이지 크롤링 중입니다.===========================")
response = requests.get(f"https://search.naver.com/search.naver?where=news&sm=tab_jump&query={keyword}&start={i}")
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 를 추출
# 다시 requests 날려준다.
response = requests.get(url, headers={'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/ 105.0.5195.102 Safari/537.36 '})
html = response.text
soup = BeautifulSoup(html, 'html.parser')
# 연예 뉴스 체크
if "entertain" in response.url:
content = soup.select_one("#articeBody")
elif "sports" in response.url:
content = soup.select_one("#newsEndContents")
# 본문 내용안에 불필요한 div, p삭제
divs = content.select("div")
for div in divs:
div.decompose()
paragraphs = content.select("p")
for p in paragraphs:
p.decompose()
else:
content = soup.select_one("#articleBodyContents")
print("==========본문===========\n", content.text.strip())
total_content += content.text.strip()
article_num = article_num + 1
time.sleep(0.3)
page_num = page_num + 1
print(f"{article_num}개 기사 크롤링 완료!!")
pyperclip.copy(total_content)
pyautogui.alert("클립보드에 복사되었습니다.")
이상입니다.답변 1
0
스타트코딩
지식공유자
AttributeError: AttributeError: 'NoneType' object has no attribute 'text 가 뜹니다.
해당 오류는 태그를 가져오지 못한 상태에서 텍스트를 추출하려 했을 때
뜨는 오류 입니다.
선택자에 오타가 있어 보이네요.
아래 부분을 찾아서 고쳐 보시기 바랍니다 ^^
if "entertain" in response.url:
content = soup.select_one("#articleBody")




