Python用Scrapy框架编写一个属于自己的爬虫

2024-11-02 13:41:26

1、在cmd中使用scrapy startproject tutorial命令创建出Scrapy模版

Python用Scrapy框架编写一个属于自己的爬虫Python用Scrapy框架编写一个属于自己的爬虫

4、import scrapyclass QuotesSpider(scrapy.Spider): na罪焐芡拂me = "quotes" def start_requests(self): urls = [ 'http://quotes.toscrape.com/page/1/' 'http://quotes.toscrape.com/page/2/' ] for url in urls: yield scrapy.Request(url=url,callback=self.parse) def parse(self,response): page = response.url.split("/")[-2] filename = 'quotes-%s.html' % page with open(filename,'wb') as f: f.write(response.body) self.log('Saved file %s' % filename)写入这些内容,这是python代码,写爬虫,起码python基本语法得能看懂

Python用Scrapy框架编写一个属于自己的爬虫

5、在顶层目录G:\Python\tutorial执行cmd命令 scrapy crawl quotes得到如下结果

Python用Scrapy框架编写一个属于自己的爬虫

7、不过细心的你肯定会发现,保存下来的网页打开来和在浏览器里直接访问的是不太一样的,这是由于Scrapy不支持js渲染,有一羿约妫鳏部分内容格式是要有js动态渲染生成的,这个可以在后期家装插件,使得Scray支持js渲染

Python用Scrapy框架编写一个属于自己的爬虫Python用Scrapy框架编写一个属于自己的爬虫
猜你喜欢