DEV Community

loading...

Day 72 Of 100DaysOfCode: CSS Locator

iamdurga profile image Durga Pokharel ・2 min read

This is my 72th day of #100DaysOfCode and #python learning. Like yesterday today also continue to learned more about web scraping in the topics text extraction, follow() method, spider, weaving the web, inspecting elements from Datacamp.

Xpath and CSS locator in python

from scrapy import Selector

# Create a selector object from a secret website
sel = Selector(  text = html)

# Select all hyperlinks of div elements belonging to class "course-block"
course_as = sel.css( 'div.course-block > a' )

# Selecting all href attributes chaining with css
hrefs_from_css = course_as.css( '::attr(href)' )

# Selecting all href attributes chaining with xpath
hrefs_from_xpath = course_as.xpath( './@href')
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide