워드 클라우드 관련 문의 드립니다.
203
작성한 질문수 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")
셀레니움 환경설정 오류
0
79
2
네이버 로그인 관련
0
359
2
안녕하세요 셀레니움에 대해서 질문
0
104
1
크롤링 연습사이트 문의
0
122
2
선택자 질문
0
89
2
'특정 요소가 나타날 때까지 스크롤' 부분 에러
0
90
2
자동 로그인 질문
0
107
2
44강 제목, 링크
0
115
1
원하는 값이 없을 때
0
106
2
크롤링한 링크가 엑셀로 들어가면 작동이 안되요
0
251
2
셀레니움 PDF자료는 받을 수 있나요
0
108
2
글목록 추출하기
0
111
2
메일 자동화 로그인 중복방지문자해결 오류 및 명시적 대기 질문
0
98
2
강의 노트가 어디에 있는건가요?
0
82
2
강의 커리큘럼 질문
0
112
1
조건문 else 사용하지 않는 이유
0
84
2
셀레니움으로 접근할 수 없는 경우
0
109
2
웹페이지 변경
0
82
2
자바스크립트로 태그 선택 시 질문입니다.
1
72
2
수료증은 어떻게 받나요?
0
129
2
class명을 활용하여 선택자를 만들지 않는 경우..?
0
67
2
드라이버가 안 열려요
0
89
2
이거 해결방법 아시는 분?
0
124
2
네이버 지식인 크롤링..
0
217
2





