一:创建scrapy项目文件
1.打开控制台(win+r)
2. 输入代码创建项目文件
scrapy startproject
项目名称
3. 进入项目(cd Get-data)
4.生成demo.py文件
本次要爬取的网站为:https://python123.io/ws/demo.html
5.打开demo文件夹![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20190910163042659.PNG?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDUwNzY4Mg==,size_16,color_FFFFFF,t_70
二、代码
1、在demo文件中修改代码
# -*- coding: utf-8 -*-
import scrapy
class DemoSpider(scrapy.Spider):
name = 'demo'
#方法一
allowed_domains = ['python123.io']
start_urls = ['http://python123.io/ws/demo.html']
#end
#方法二
# def start_requests(self):
# urls = [
# 'http://python123.io/ws/demo.html'
# ]
# for url in urls:
# yield scrapy.Request(url=url callback=self.parse)
#end
def parse(self, response):
#pass
fname = response.url.split('/')[-1]
with open(fname,'wb') as f:
f.write(response.body)
self.log('Saved file %$.' % fname)
2.运行demo.py
运行后可以看到已经生成的demo.html(爬取成功)