爬取数据时候,不加索引[0]也能爬取。为什么要加[0]
来源:5-5 案例-爬取求职网站的招聘信息
qq_阳光_134
2020-07-08 22:25:14
for item in all_div:
info = {}
info['job_name'] = item.xpath("./p/span/a/@title")[0]
info['company_name'] = item.xpath(".//span[@class='t2']/a/@title")[0]
info["company_addrees"] = item.xpath(".//span[@class='t3']/text()")
info["money"] = item.xpath(".//span[@class='t4']/text()")
info["date"] = item.xpath(".//span[@class='t5']/text()")
info_list.append(info)
1回答
同学,你好。此处是可以不加索引[0]的,因为得到的列表数据里面只有一个,如图:
加上索引是取出列表中的第一个数据,结果是一样的,这可能来源于不同的编程习惯,同学可根据自己的方式去编写。
如果解决了你的疑惑,请采纳,祝学习愉快~
相似问题