7

Python爬虫编程思想(150):使用Scrapy抓取数据,并将抓取到的数据保存为多种格式的...

 2 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/125250642
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python爬虫编程思想(150):使用Scrapy抓取数据,并将抓取到的数据保存为多种格式的文件

蒙娜丽宁 于 2022-06-12 21:54:57 发布 14

        parse方法的返回值会被传给Item Pipeline,并由相应的Item Pipeline将数据保存成相应格式的文件。parse方法必须返回Item类型的数据。也就是说,parse方法的返回值类型必须是scrapy.Item或其scrapy.Item的子类。在该类中会定义与要保存的数据对应的属性。

        下面的例子首先会定义一个Item类,在该类中会定义title、href和abstract三个属性,然后在parse方法中返回Item类的实例,并设置这3个属性的值。最后在运行网络爬虫时会通过“-o”命令行参数指定保存的文件类型(通过扩展名指定),成功运行后,就会将抓取到的数据保存到指定的文件中。

        首先要在items.py脚本文件中编写一个Item类,在创建Scrapy工程时,items.py脚本文件中已经有了一个MyscrapyItem类了,我们可以直接利用这个类。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK