Webscraping in Python
Thomas Laetsch
Data Scientist, NYU
/ door > (behalve het eerste teken)/html/body/divhtml > body > div// door een spatie (behalve het eerste teken)//div/span//pdiv > span p[N] door :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 selecteert alle paragrafen met class-1#div#uid selecteert het div-element met id gelijk aan uidSelecteer paragrafen binnen class class1:
css_locator = 'div#uid > p.class1'
Selecteer alle elementen met class 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")
uit: [<Selector xpath='...' data='<p>Hello World!</p>'>]
>>> sel.css("div > p").extract()
uit: [ '<p>Hello World!</p>' ]
Webscraping in Python