写在前面:
由于优快云的审查机制的原因,更多博客内容请访问我的个人博客或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)))

本文介绍了一种使用XPath和Python循环来高效抓取网页列表的方法,通过动态改变XPath路径中的数字参数,实现对列表中多个元素的快速定位和数据抓取。
22万+

被折叠的 条评论
为什么被折叠?



