inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[신규 개정판] 이것이 진짜 크롤링이다 - 실전편 (인공지능 수익화)

속성으로 선택하기

좋은 강의 잘 듣고 있습니다.

225

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)

python 웹-크롤링

답변 1

0

스타트코딩

imageh2 태그의 선택자를 만들어서 찾아 오면 됩니다 ^^

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

46

2

네이버 로그인 관련

0

255

2

안녕하세요 셀레니움에 대해서 질문

0

85

1

크롤링 연습사이트 문의

0

96

2

선택자 질문

0

71

2

'특정 요소가 나타날 때까지 스크롤' 부분 에러

0

76

2

자동 로그인 질문

0

86

2

44강 제목, 링크

0

105

1

원하는 값이 없을 때

0

89

2

크롤링한 링크가 엑셀로 들어가면 작동이 안되요

0

227

2

셀레니움 PDF자료는 받을 수 있나요

0

100

2

글목록 추출하기

0

97

2

메일 자동화 로그인 중복방지문자해결 오류 및 명시적 대기 질문

0

88

2

강의 노트가 어디에 있는건가요?

0

80

2

강의 커리큘럼 질문

0

98

1

조건문 else 사용하지 않는 이유

0

76

2

셀레니움으로 접근할 수 없는 경우

0

95

2

웹페이지 변경

0

70

2

자바스크립트로 태그 선택 시 질문입니다.

1

64

2

수료증은 어떻게 받나요?

0

119

2

class명을 활용하여 선택자를 만들지 않는 경우..?

0

60

2

드라이버가 안 열려요

0

79

2

이거 해결방법 아시는 분?

0

121

2

네이버 지식인 크롤링..

0

201

2