Web Scraping in Python
Thomas Laetsch
Data Scientist, NYU
/ durch > ersetzen (außer am Anfang)/html/body/divhtml > body > div// durch Leerzeichen ersetzen (außer am Anfang)//div/span//pdiv > span p[N] durch :nth-of-type(N) ersetzen//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 wählt alle p-Elemente mit class-1#div#uid wählt das div mit id gleich uidAbsätze innerhalb der Klasse class1 auswählen:
css_locator = 'div#uid > p.class1'
Alle Elemente mit Klassenattribut class1 auswählen:
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>' ]
Web Scraping in Python