Reputation: 22440
I have created a scraper using python which is parsing links from a webpage. It scrapes 10 links from that site. Is it possible if i want to parse the first 8 links or the last 8 links out of that 10 links? Actually, I can't get any idea how to do so. Any help would be highly appreciated.
import requests
from lxml import html
url = "http://www.supplyhouse.com/"
def Selectivelinks(address):
response = requests.get(address)
tree = html.fromstring(response.text)
titles = tree.xpath('//ul[@id="shop-by-category-list"]')
for title in titles:
links=title.xpath('.//a/@href')
for lnk in links:
print(lnk)
Selectivelinks(url)
Upvotes: 0
Views: 58
Reputation:
If links
returns a list
. Then you can fetch the last 8 links
using links[:-8]
Consider x
contains a list
of numbers from 1-10 then x[-8:]
will return the last 8 items in the list
x = [i for i in range (0, 10)]
print x[-8:]
# [2, 3, 4, 5, 6, 7, 8, 9]
Also known as list slicing.
Upvotes: 1