inflearn logo
강의

Course

Instructor

Introduction to Python and Creating Various Automated Applications Using Web Crawling

How to use BeautifulSoup and Simple Web Parsing Practice (1) - Naver, Daum, Inflearn

과제 질문 하나 드립니다!

201

lgs95050923

3 asked

0

안녕하세요 선생님. 강좌 잘 보고있습니다.

다름이 아니라 이번 과제에 질문이 한가지 있는데요.

from bs4 import BeautifulSoup

import urllib.request as req

import io

import sys

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')

sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

url = "https://www.daum.net"

url_req = req.urlopen(url).read()

url_parse = BeautifulSoup(url_req, "html.parser")

hot_issue = url_parse.select_one("ol.list_hotissue.issue_row").select("a.link_issue")

for number, issue in enumerate(hot_issue, 1):

print(number, issue.attrs['href'], " + ", issue.string)

이렇게 코드를 작성했는데요. 실행을 하면 두개씩 뜹니다.

1위가 2개, 2위가 2개, 3위가 2개.. 아래와 같은 식이요.

1 https://search.daum.net/search?w=tot&q=%EB%8F%99%EB%A7%89%EA%B3%A8+%EC%86%8C%EB%85%80&DA=ATG&rtmaxcoll=1TH + 동막골 소녀

2 https://search.daum.net/search?w=tot&q=%EB%8F%99%EB%A7%89%EA%B3%A8+%EC%86%8C%EB%85%80&DA=ATG&rtmaxcoll=1TH + 동막골 소녀

3 https://search.daum.net/search?w=tot&q=%EC%86%94%EC%A7%80&DA=ATG&rtmaxcoll=1TH + 솔지

4 https://search.daum.net/search?w=tot&q=%EC%86%94%EC%A7%80&DA=ATG&rtmaxcoll=1TH + 솔지

.....

그래서 크롬 개발자도구를 열어보니까

<a href="링크" class="link_issue">추전역이 있고

<a href="링크" class="link_issue" tableindex="-1">추전역도 있어서

두개 전부 잡아내는거같은데 이럴경우엔 어떻게해야할까요ㅜㅜ

python 웹-크롤링

Answer 1

0

niceman

안녕하세요. 이건수님

잘하시고 계십니다. 현재 선택자를 보니 2개가 선택되는 것이 맞습니다.

  1. 인덱스로 1개에만 접근 하거나.
  2. 한 개만 가져올 수 있도록 선택자 셀렉트 부분을 수정하셔야 합니다.

아래와 같이 작성하시면 1개만 가져올 듯합니다.

=============================================================

from bs4 import BeautifulSoup

import urllib.request as req

import sys

import io

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = ‘utf-8’)

sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = ‘utf-8’)

url = “https://www.daum.net/

res = req.urlopen(url).read()

soup = BeautifulSoup(res,”html.parser”)

print(‘soup’,soup.prettify())

top10 = soup.find_all(“a”, tabindex=”-1″)

for i,e in enumerate(top10,1):

print(i,e.string)

=============================================================

급한 질문이 생기면 쪽지로 부탁드립니다.

감사합니다.!!

현재 예제에서 error 발생

0

367

3

유튜브 동영상 다운로드

0

1448

2

Atom 에디터 관련

0

337

1

위시켓 폼데이터

0

274

1

스케줄러 사용 관련 질문 드립니다

0

627

1

selenium 에러

0

428

1

Progress bar 쓰레드 관련

0

489

1

Install Package 관련 문의

0

328

1

tkinter 샘플 코드 실행 오류 건

0

1267

1

4-7-6 네이버 & 카카오 주식 정보 가져오기

0

381

1

네이버자동로그인_by_selenium

0

876

1

위시캣 로그인 처리 및 크롤링 질문

0

345

1

2-8-1 네이버이미지 크롤링 질문

1

604

3

li:nth-of-type 질문

0

350

2

에러가 뜨는데 잘 모르겠어요ㅠ

0

401

2

Install Packages 항목이 안보이는 이유가 뭘까요?

0

400

2

환경변수 Path 설정 방법

0

631

1

웹 브라우저 없는 스크랩핑 및 파싱 실습(1) - 인프런

0

333

1

웹 브라우저 없는 스크랩핑 및 파싱 실습(1) -git주소

0

475

3

download2-8-1. py질문

0

203

1

ip 차단 당하는 거 같은데 아무리 랜덤주고, sleep 줘도 안 되는데 다른 방법 더 있을까요??

0

645

1

인프런 환경이 바뀌어서 제나름대로 하는데

0

196

1

다시올려주신 예제파일로하는데

0

190

1

아직도 에러가뜨나보네요?

0

584

1