inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

불필요한 div, p 코드 삽입 후 에러

235

yhahn02

작성한 질문수 3

0

안녕하세요. 샘

불필요한 div, p 코드 사입 후 에러 발생 건 입니다.

 

스크린샷 2023-10-01 오후 6.59.46.png

import requests
from bs4 import BeautifulSoup
import time

req_header_dict = {
    # 요청헤더 : 브라우저 정보
    'user-agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
}

response = requests.get("https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%EC%86%90%ED%9D%A5%EB%AF%BC", headers= req_header_dict)
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:
        url = links[1].attrs["href"]
        response = requests.get(url, headers= req_header_dict)
        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")
        # 본문 내용안에 불필요한 dvi 삭제
            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(".tit.title_area")
            content = soup.select_one("#newsct_article")
        

        print("##########링크##########",url)
        print("##########제목##########",title.text.strip())
        print("##########본문##########",content.text.strip())



        time.sleep(0.3)

python 웹-크롤링

답변 1

0

스타트코딩

아래 선택자에 오류가 있습니다~

알맞게 바꿔 보세요 ㅎㅎ

title = soup.select_one(".tit.title_area")

셀레니움 환경설정 오류

0

45

2

네이버 로그인 관련

0

252

2

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

0

84

1

크롤링 연습사이트 문의

0

95

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

97

1

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

0

75

2

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

0

95

2

웹페이지 변경

0

70

2

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

1

64

2

수료증은 어떻게 받나요?

0

117

2

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

0

60

2

드라이버가 안 열려요

0

79

2

이거 해결방법 아시는 분?

0

121

2

네이버 지식인 크롤링..

0

201

2