from parsel import Selector import requests # Fetch webpage data response = requests.get('http://www.example.com') html = response.text # Create Selector object with fetched data sel = Selector(text=html) # Extract all links into a list links = sel.css('a::attr(href)').getall() print(links)
from parsel import Selector import requests # Fetch webpage data response = requests.get('http://www.example.com') html = response.text # Create Selector object with fetched data sel = Selector(text=html) # Extract table rows into a list rows = sel.css('tr').getall() # Extract text from each row for row in rows: data = Selector(text=row).css('td::text').getall() print(data)This code uses the Selector object to extract data from a table on a webpage. The `sel.css()` method is used to create a CSS selector that targets all table rows on the page. Then, a loop is used to iterate through each row and extract the text from the cells using another CSS selector. Both examples use the ParSel package library to parse HTML and XML documents.