좋은 강의 잘 듣고 있습니다.
227
chlee1016
작성한 질문수 2
0
저는 테슬라 키워드로 응용을 하고 있었는데요, 아래 기사에서
https://www.yna.co.kr/view/AKR20230706003700075?input=1195m
title을 어떻게 가져와야 할지, 일반화 되는 방법을 아무리 봐도 잘 모르겠습니다 ㅠ 본문은 #contents로 가져왔습니다. 도와주세요..
import requests
import time
from bs4 import BeautifulSoup
response = requests.get("https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%ED%85%8C%EC%8A%AC%EB%9D%BC")
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) > 1: # 링크가 2개 이상이면
url = links[1].attrs['href'] # 두번째 링크의 herf를 추출
# requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')) 방지를 위해 header 추가
response = requests.get(url, headers={'User-agent': 'Mozila/5.0'})
html = response.text
soup = BeautifulSoup(html, "html.parser")
content = soup.select_one("#contents")
print(content.text)
time.sleep(0.3)
답변 1
0
h2 태그의 선택자를 만들어서 찾아 오면 됩니다 ^^
import requests
import time
from bs4 import BeautifulSoup
response = requests.get("https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%ED%85%8C%EC%8A%AC%EB%9D%BC")
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) > 1: # 링크가 2개 이상이면
url = links[1].attrs['href'] # 두번째 링크의 herf를 추출
# requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')) 방지를 위해 header 추가
response = requests.get(url, headers={'User-agent': 'Mozila/5.0'})
html = response.text
soup = BeautifulSoup(html, "html.parser")
title = soup.select_one("#title_area") # 제목
content = soup.select_one("#contents")
print(title.text, content.text)
time.sleep(0.3)
셀레니움 환경설정 오류
0
80
2
네이버 로그인 관련
0
366
2
안녕하세요 셀레니움에 대해서 질문
0
104
1
크롤링 연습사이트 문의
0
122
2
선택자 질문
0
89
2
'특정 요소가 나타날 때까지 스크롤' 부분 에러
0
90
2
자동 로그인 질문
0
107
2
44강 제목, 링크
0
115
1
원하는 값이 없을 때
0
106
2
크롤링한 링크가 엑셀로 들어가면 작동이 안되요
0
252
2
셀레니움 PDF자료는 받을 수 있나요
0
108
2
글목록 추출하기
0
111
2
메일 자동화 로그인 중복방지문자해결 오류 및 명시적 대기 질문
0
99
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
90
2
이거 해결방법 아시는 분?
0
124
2
네이버 지식인 크롤링..
0
217
2





