scapy框架学习

scrapy startproject mySpider  创建爬虫

scrapy crawl myspider  运行代码

scrapy crawl myspider -o myspider.json  将服务器的内容生成json文件

爬虫的目录结构:

└── mySpider
   ├── mySpider
   │   ├── __init__.py
   │   ├── items.py
   │   ├── middlewares.py
   │   ├── pipelines.py       管道文件
   │   ├── settings.py        配置文件
   │   └── spiders            爬虫代码目录

   │       ├── __init__.py


在spiders 目录下生成文件

我们自己的爬虫代码所在位置

class aaaSpider(scrapy.Spider):    

    "运行爬虫时的名字"                       

    name = "myspider "                                              
    "允许爬虫作用的范围"                                         
    allowd_domains = ["http://www.xxxcn/"]                   
    "爬虫起始的url"                                              
    start_urls = ["http://www.xxx.cn/channel/xxxx.shtml#"] 
                                                                 
    “处理下载文件的方法 固定名字”                                                                 

    def parse(selfi,response):      


setting.py 文件中:

设置http报文头

    DEFAULT_REQUEST_HEADERS = {                                                        
    'User-Agent' : 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
',                                                                                 
   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',    
   'Accept-Language': 'en',                                                        

}             

    “设置管道文件”                                                                     

      ITEM_PIPELINES = {                             
    'mySpider.pipelines.MyspiderPipeline': 300,

}         


pipelines.py 文件:

用来处理服务器返回的内容,将内容写入文件

 class MyspiderPipeline(object):                                       
     def __init__(self):                                               
         self.filename = open("xxxx.json","w")                      
                                                                       
     def process_item(self,item,spider):                               
         jsontext = json.dumps(dict(item),ensure_ascii = False) + "\n" 
         self.filename.write(jsontext.encode("utf-8"))                 
         return item                                                   
                                                                       
     def close_spider(self,spider):                                    
         sefl.filename.close()                                                                                                   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值