… This is different from xpath = '//*[@class="class1"]' which forces an exact match. Also different from using contains xpath = '//*[contains(@class,"class1")]' which snatches a string.
To find all the children of an element whose id is equal to “uid”: css_locator = "#uid > *" Must remember to add the star to follow through to those child elements.
To select displayed text on websites: XPath: <xpath-to-element>/@attr-name xpath = '//div[@id="uid"]/a/@href' CSS:
::attr(attr-name) css_locator = 'div#uid > a::attr(href)' The double colon selects the desired attribute (that which is in between the quotes); a web address in this case
Text Extraction In some instances, you pay want to extract text from an element. For example: Hello world! Try DataCamp today!
We may just want to extract the text. To do this, we need to navigate to the paragraph id that is equal to “p-example”
sel.xpath('//p[@id="p-example"]/text()').extract() The above line takes a scrappy selector with an xpath that navigates to what we need. It extracts the following: ['\n Hello world!\n Try ', ' today!\n']
sel.xpath('//p[@id="p-example"]//text()').extract() The above results: ['\n Hello world!\n Try ', 'DataCamp', ' today!\n']