본문 바로가기
파이썬

파이썬 BeautifulSoup을 사용하여 클래스에있는 특정 링크를 검색하기 위해 CSS 선택기를 사용하는 방법은 무엇입니까?

by º기록 2020. 12. 12.
반응형


from bs4 import BeautifulSoup

import requests

url = "http://allevents.in/lahore/"

r  = requests.get(url)

data = r.text

soup = BeautifulSoup(data)
for link in soup.select( 'html body div.non-overlay.gray-trans-back div.container div.row div.span8 div#eh-1748056798.events-horizontal div.eh-container.row ul.eh-slider li.h-item div.h-meta div.title a[href]'):
    print link.get('href')

 

해결 방법

 

이 페이지는 클래스와 마크 업을 사용하는 데 가장 친숙하지는 않지만 CSS 선택기가 너무 구체적이어서 여기서 유용하지 않습니다.


upcoming_events_div = soup.select_one('div#events-horizontal')
for link in upcoming_events_div.select('div.title a[href]'):
    print link['href']


 

참조 페이지 https://stackoverflow.com/questions/24801548

 

 

반응형

댓글