• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

과제 질문 하나 드립니다!

18.09.23 18:53 작성 조회수 116

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">추전역도 있어서

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

답변 1

답변을 작성해보세요.

0

안녕하세요. 이건수님

잘하시고 계십니다. 현재 선택자를 보니 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)

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

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

감사합니다.!!