【爬虫】在 xpath路径中插入变量的正确姿势

举例说明

"""说明:要在列表a中分别插入三个内容,且这三个内容只有tbody[1]中的数字不同,分别为1, 2, 3"""
//源代码
a = []
a.insert(0, dom.xpath("//div[@class='listWraper']/table[2]/tbody[1]/tr[1]/td[1]/p[1]/text()"))
a.insert(1, dom.xpath("//div[@class='listWraper']/table[2]/tbody[2]/tr[1]/td[1]/p[1]/text()"))
a.insert(2, dom.xpath("//div[@class='listWraper']/table[2]/tbody[3]/tr[1]/td[1]/p[1]/text()"))
"""这里用循环来来实现三条内容的插入"""
a = []
for i in range(3):
	# 这里用{}.format(i)的方式来在xpath路径中插入变量
	a.insert(i, dom.xpath("//div[@class='listWraper']/table[2]/tbody[{}]/tr[1]/td[1]/p[1]/text()".format(i+1)))

猜你喜欢

转载自blog.csdn.net/IT_SoftEngineer/article/details/107750452
今日推荐