我使用 python 创建了一个抓取器,它解析网页中的链接。它从该站点抓取了 10 个链接。如果我想解析这 10 个链接中的前 8 个链接或最后 8 个链接,是否有可能?实际上,我不知道该怎么做。任何帮助将不胜感激。
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)
如果links
返回一个list
. 然后你就可以读取最后8links
使用links[:-8]
考虑x
包含list
1-10 个数字,然后x[-8:]
将返回最后 8 个项目list
x = [i for i in range (0, 10)]
print x[-8:]
# [2, 3, 4, 5, 6, 7, 8, 9]
也称为列表切片。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句