from parsel import Selector import requests # send http request to the server and get the response response = requests.get('http://www.example.com') # create selector object selector = Selector(response.text) # xpath title = selector.xpath('//title/text()').get() print(title)
from parsel import Selector import requests response = requests.get('http://books.toscrape.com/') # create selector object selector = Selector(response.text) # css selector articles = selector.css('article.product_pod') for article in articles: title = article.css('h3 a::attr(title)').get() price = article.css('div p.price_color::text').get() print(title) print(price)This example shows how to use the parsel selector to extract the titles and prices of books from a web page using CSS selectors. The `Selector` object is initialized with the HTML content obtained from the HTTP GET request. The `css()` method is then used to select elements using a CSS selector. The `::attr()` and `::text` pseudo-selectors are used to extract attributes and text values of selected elements. In conclusion, the examples above show how to use the parsel selector to extract data from web pages using XPath and CSS selectors. The parsel library is a package that serves as a convenient tool for web scraping tasks in Python.