写在前面:
由于CSDN的审查机制的原因,更多博客内容请访问我的个人博客或GitHub:
举例说明
"""说明:要在列表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)))