Python으로 하는 웹 스크레이핑
Thomas Laetsch
Data Scientist, NYU
/ 는 > 로 바꿉니다/html/body/divhtml > body > div// 는 공백으로 바꿉니다//div/span//pdiv > span p[N] 는 :nth-of-type(N) 으로 바꿉니다//div/p[2]div > p:nth-of-type(2)XPATH
xpath = '/html/body//div/p[2]'
CSS
css = 'html > body div > p:nth-of-type(2)'
. 사용p.class-1 은 class-1 인 모든 p 요소를 선택# 사용div#uid 는 id 가 uid 인 div 요소를 선택클래스 class1 내부의 p 요소 선택:
css_locator = 'div#uid > p.class1'
클래스 속성이 class1 인 모든 요소 선택:
css_locator = '.class1'
css = '.class1'

xpath = '//*[@class="class1"]'

xpath = '//*[contains(@class,"class1")]'

from scrapy import Selector
html = '''
<html>
<body>
<div class="hello datacamp">
<p>Hello World!</p>
</div>
<p>Enjoy DataCamp!</p>
</body>
</html>
'''
sel = Selector( text = html )
>>> sel.css("div > p")
out: [<Selector xpath='...' data='<p>Hello World!</p>'>]
>>> sel.css("div > p").extract()
out: [ '<p>Hello World!</p>' ]
Python으로 하는 웹 스크레이핑