zoukankan      html  css  js  c++  java
  • Scrapy爬虫框架的使用

    #_author:来童星
    #date:2019/12/24
    # Scrapy爬虫框架的使用
    #1.安装Twisted模块 https://www.lfd.uci.edu/~gohlke/pythonlibs/
    #2.单击Twisted索引
    import scrapy
    from scrapy.crawler import CrawlerProcess
    # 导入获取项目设置信息
    from scrapy.utils.project import get_project_settings
    class QuotesSpider(scrapy.Spider):
    name='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:
    # 向文件中写入获取的html代码
    f.write(response.body)
    #输出保存文件的名称
    self.log('saved file %s'%filename)
    if __name__=='__main__':
    #创建CrawlerProcess类对象并传入项目设置信息参数
    process=CrawlerProcess(get_project_settings())
    # 设置需要启动的爬虫名称
    process.crawl('quotes')
    process.start()
  • 相关阅读:
    0722
    SGU
    预测一下吧
    0625
    0624
    0610
    0607
    0604
    poj2455Secret Milking Machine【二分 + 最大流】
    BZOJ3489: A simple rmq problem
  • 原文地址:https://www.cnblogs.com/startl/p/12092627.html
Copyright © 2011-2022 走看看