zoukankan      html  css  js  c++  java
  • scrapy 爬取糗事百科

    • 安装scrapy

    conda install scrapy

    • 创建scrapy项目

    scrapy startproject qiubai

    image

    • 启动pycharm,发现新增加了qiubai这个目录

    image

    • 在spider目录下创建indexpage.py文件

    image

    image

    • 编写糗百爬虫,获取首页的所有作者信息
    #导入scrapy
    import scrapy
    
    #创建糗百爬虫类
    class QiuBaiSpider(scrapy.Spider):
        #定义爬虫的名字
        name = 'qiubai'
        #定义爬虫开始的URL
        start_urls=['http://www.qiushibaike.com/',]
    
        #处理爬取的信息
        def parse(self, response):
            li=response.xpath('//div[@class="author clearfix"]/a[2]/h2/text()').extract()
            #li=response.xpath("//h2/text()").extract()
            for item in li:
                print item
    • 在和scrapy.cfg同级的目录下创建manage.py

    image

    image

    输入代码

    from scrapy.cmdline import execute
    
    execute()
    • 配置运行参数

    image

    image

    USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'
    • 运行爬虫
    image
  • 相关阅读:
    Emacs教程
    华为上机测试 2015
    奇偶排序
    C语言中的EOF和回车不一样
    jquery 使用方法
    1116
    1115
    1109
    Oracle14~23
    get与post的区别
  • 原文地址:https://www.cnblogs.com/yanhongjun/p/5361391.html
Copyright © 2011-2022 走看看