소개
게시글
질문&답변
2023.09.20
네이버 뉴스본문 가지고 오기도 되고 연예뉴스도 되는데 스포츠 뉴만 안되네요?
import requests from bs4 import BeautifulSoup import time import pyautogui keyword = pyautogui.prompt("검색어를 입력 하세요") response = requests.get(f"https://search.naver.com/search.naver?sm=tab_sug.top&where=news&query={keyword}") 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 "entertain" in response.url: title = soup.select_one(".end_tit") content = soup.select_one ("#articeBody") elif "sports" in response.url: title = soup.select_one("h4.title") content = soup.select_one ("#newsEndContents") # 본문 내용안애 불필요한 div삭제 divs = content.select("div") for div in divs: div.decompose() paragraphs = content.select("p") for p in paragraphs: p.decompose() else: title = soup.select_one("#artcleTitle") content = soup.select_one("#areicleBodyContents") print("============링크=========\n", url) print("============제목=========\n", title.text.strip()) print("============본문=========\n", content.text.strip()) time.sleep(0.3) AttributeError: 'NoneType' object has no attribute 'text'(base) tonyahn@Tonyui-MacBookPro vscode % /Users/tonyahn/anaconda3/bin/python /Users/tonyahn/Desktop/my_tony/vscode/Capter04/05.검색어변경하기2023-09-20 15:55:35.653 python[10820:73580] TSM AdjustCapsLockLEDForKeyTransitionHandling - _ISSetPhysicalKeyboardCapsLockLED Inhibit============링크========= https://n.news.naver.com/mnews/article/277/0005312599?sid=102Traceback (most recent call last): File "/Users/tonyahn/Desktop/my_tony/vscode/Capter04/05.검색어변경하기", line 38, in print("============제목=========\n", title.text.strip()) ^^^^^^^^^^AttributeError: 'NoneType' object has no attribute 'text'(base) tonyahn@Tonyui-MacBookPro vscode % /Users/tonyahn/anaconda3/bin/python /Users/tonyahn/Desktop/my_tony/vscode/Capter04/05.검색어변경하기2023-09-20 15:57:04.370 python[10846:74715] TSM AdjustCapsLockLEDForKeyTransitionHandling - _ISSetPhysicalKeyboardCapsLockLED Inhibit============링크========= https://n.news.naver.com/mnews/article/008/0004940773?sid=101Traceback (most recent call last): File "/Users/tonyahn/Desktop/my_tony/vscode/Capter04/05.검색어변경하기", line 38, in print("============제목=========\n", title.text.strip()) ^^^^^^^^^^AttributeError: 'NoneType' object has no attribute 'text'(base) tonyahn@Tonyui-MacBookPro vscode % 질문 : 검색어변경하기 후 이런 현상이 발생 합니다.
- 0
- 2
- 499
질문&답변
2022.10.10
네이버 주식 크롤링 (1) 질문드립니다.
저도 동일한 오류가 발행 합니다. 네이버 주식 크롤링 (1) 질문드립니다.bArgument 'which' was left unspecified. Choosing first table. 라고 에러가 나오면 어떡해야 하나요? library(httr)library(rvest)library(htmltab)library(XML)i final_data for(i in 1:10){ url https://finance.naver.com/item/sise_day.nhn?code=005930&page=",i) b b final_data cat("\n",i)}
- 0
- 3
- 245