Öznitelik ve Metin Seçimi

Python ile Web Scraping

Thomas Laetsch

Data Scientist, NYU

İki Noktayı Cesurca Kullanın

  • XPath kullanımı: <xpath-to-element>/@attr-name
xpath = '//div[@id="uid"]/a/@href'
  • CSS konumlayıcı kullanımı: <css-to-element>::attr(attr-name)
css_locator = 'div#uid > a::attr(href)'
Python ile Web Scraping

Metin Çıkarma

<p id="p-example">
  Hello world! 
  Try <a href="http://www.datacamp.com">DataCamp</a> today!
</p>
  • XPath için text() kullanın
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']
Python ile Web Scraping

Metin Çıkarma

<p id="p-example">
  Hello world! 
  Try <a href="http://www.datacamp.com">DataCamp</a> today!
</p>
  • CSS konumlayıcı için ::text kullanın
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']
Python ile Web Scraping

İki Noktanın Kapsamı

Python ile Web Scraping

Preparing Video For Download...