Attributen en tekst selecteren

Webscraping in Python

Thomas Laetsch

Data Scientist, NYU

Durf je dubbelepunt te gebruiken

  • Met XPath: <xpath-to-element>/@attr-name
xpath = '//div[@id="uid"]/a/@href'
  • Met CSS-locator: <css-to-element>::attr(attr-name)
css_locator = 'div#uid > a::attr(href)'
Webscraping in Python

Tekstextractie

<p id="p-example">
  Hello world! 
  Try <a href="http://www.datacamp.com">DataCamp</a> today!
</p>
  • In XPath gebruik je text()
sel.xpath('//p[@id="p-example"]/text()').extract()

# result: ['\n Hello world!\n Try ', ' today!\n']
sel.xpath('//p[@id="p-example"]//text()').extract()

# result: ['\n Hello world!\n Try ', 'DataCamp', ' today!\n']
Webscraping in Python

Tekstextractie

<p id="p-example">
  Hello world! 
  Try <a href="http://www.datacamp.com">DataCamp</a> today!
</p>
  • Voor CSS-locator gebruik je ::text
sel.css('p#p-example::text').extract()

# result: ['\n Hello world!\n Try ', ' today!\n']
sel.css('p#p-example ::text').extract()

# result: ['\n Hello world!\n Try ', 'DataCamp', ' today!\n']
Webscraping in Python

De scope van de dubbelepunt

Webscraping in Python

Preparing Video For Download...