CSS लोकेटर्स

Python में Web Scraping

Thomas Laetsch

Data Scientist, NYU

Rosetta CSStone

  • / को > से बदलें (पहला अक्षर छोड़कर)
    • XPath: /html/body/div
    • CSS Locator: html > body > div
  • // को खाली स्पेस से बदलें (पहला अक्षर छोड़कर)
    • XPath: //div/span//p
    • CSS Locator: div > span p
  • [N] को :nth-of-type(N) से बदलें
    • XPath: //div/p[2]
    • CSS Locator: div > p:nth-of-type(2)
Python में Web Scraping

Rosetta CSStone

XPATH

xpath = '/html/body//div/p[2]'

CSS

css = 'html > body div > p:nth-of-type(2)'
Python में Web Scraping

CSS में Attributes

  • क्लास से एलिमेंट ढूँढने के लिए, डॉट . का उपयोग करें
    • उदाहरण: p.class-1 उन सभी पैराग्राफ एलिमेंट्स को चुनता है जो class-1 में हैं
  • id से एलिमेंट ढूँढने के लिए, पाउंड साइन # का उपयोग करें
    • उदाहरण: div#uid वह div एलिमेंट चुनता है जिसका id uid है
Python में Web Scraping

CSS में Attributes

class1 क्लास के भीतर पैराग्राफ एलिमेंट्स चुनें:

css_locator = 'div#uid > p.class1'

सभी एलिमेंट्स चुनें जिनका class attribute class1 है:

css_locator = '.class1'
Python में Web Scraping

क्लास स्टेटस

css = '.class1'

ClassSelection-Xpath-classonly.png

Python में Web Scraping

क्लास स्टेटस

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

ClassSelection-Xpath-eq.png

Python में Web Scraping

क्लास स्टेटस

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

ClassSelection-Xpath-contains.png

Python में Web Scraping

CSS के साथ सेलेक्टर्स

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 में Web Scraping

C(SS) You Soon!

Python में Web Scraping

Preparing Video For Download...