scrapy步骤记录

2019-08-21  本文已影响0人  天雨流芳hodo

1.创建项目:scrapy startproject 项目名
如果是想在对应pycharm目录下创建,则在pycharm中选择-show in explorer,在需要创建项目的文件夹中打开cmd(shift+此处打开powershell)执行创建项目命令

2.创建爬虫,进入项目文件夹下(文件夹包含两个文件,其中为scrapy.cfg),cmd中运行
scrapy genspider 爬虫名 网站域名

3.编写item字段(items.py文件)

4.编写爬虫程序:采集数据,翻页操作等

5.编写pipelins.py文件,存储数据,如:

def __init__(self):
    self.f = open("tencent.json","w")

def process_item(self, item, spider):
    line = json.dumps(dict(item)) + "\n"
    self.f.write(line)
    return item

def close_spider(self,spider):
    self.f.close()

6.设置setting文件

IMAGES_STORE = "D:/test/"

7.进入项目目录命令行中运行爬虫
scrapy crawl 爬虫名

上一篇 下一篇

猜你喜欢

热点阅读