zoukankan      html  css  js  c++  java
  • 【python爬虫】scrapy入门1--环境搭建

    Scrapy Day01

    1)

    进入主目录,右键打开终端,创建项目

    scrapy startproject xicidailiSpyder

    进入项目目录

    cd xicidailiSpyder/

    创建爬虫文件(文件名不能与项目名相同)

    scrapy genspider xicidaili www.xicidaili.com

     

    2)

    Settings.py

     

    取消注释:ROBOTSTXT_OBEY = False

    取消注释:ITEM_PIPELINES

    取消注释:DEFAULT_REQUEST_HEADERS,添加'User-Agent’:用户代理

    设置输出编码(csv中文乱码特效药):FEED_EXPORT_ENCODING = 'utf-8-sig'

     

    3)

    爬虫文件名.py

    修改:start_urls = ['http://www.xicidaili.com/nt/6']

    修改:

    def parse(self, response):

            # pass

            selectors=response.xpath('//tr')

            for selector in selectors:

                ip=selector.xpath('./td[2]/text()').get()

                port=selector.xpath('./td[3]/text()').get()

                # print(ip,port)

                items ={

                    'ip':ip,

                    'port':port

                    }

                # yield:跟字典

                yield items

            next_page=response.xpath("//a[@class='next_page']/@href").get()

            if next_page:

                print(next_page)

                next_url=response.urljoin(next_page)

                # 发出请求 Request,callback 回调函数 将请求得到的响应交给自己处理 

                yield scrapy.Request(next_url,callback=self.parse)

    4)

    开始爬虫

    scrapy crawl xicidaili 

    导出数据格式

    scrapy crawl xicidaili -o ip.json

    scrapy crawl xicidaili -o ip.csv

     

    注意这3个命令都是项目相关的,只能用于已存在的项目。

     

    其他:

    1、谷歌插件:XPath Helper

    2、pip install scrapy,依赖包twisted错误,第三方库离线下载whl(搜索twisted):https://pypi.org/search/?q=twisted&o=

    3、硬件测试命令:scrapy bench,错误需要安装:pip install pywin32

  • 相关阅读:
    POJ3320 Jessica's Reading Problem
    POJ3320 Jessica's Reading Problem
    CodeForces 813B The Golden Age
    CodeForces 813B The Golden Age
    An impassioned circulation of affection CodeForces
    An impassioned circulation of affection CodeForces
    Codeforces Round #444 (Div. 2) B. Cubes for Masha
    2013=7=21 进制转换
    2013=7=15
    2013=7=14
  • 原文地址:https://www.cnblogs.com/hightech/p/12839685.html
Copyright © 2011-2022 走看看