spiders --- top17173

# -*- coding: utf-8 -*-
import scrapy
from demo1.items import GameItem

class Top17173Spider(scrapy.Spider):
    name = 'top17173'
    allowed_domains = ['top17173.com']
    start_urls = ['http://top.17173.com/list-0-0-0-0-0-0-0-0-0-0-1.html','http://top.17173.com/list-0-0-4-0-0-0-0-0-0-0-1.html','http://top.17173.com/vr-index.shtml']

    def parse(self, response):
        name = response.xpath('//div[@class="item-in"]/div[@class="c2"]//a/text()').extract()
        poll = response.xpath('//div[@class="item-in"]/div[@class="c3"]/text()').extract()
        polls = []
        names =  []
        for p in poll:
            p = ''.join(p.split())
            polls.append(p)

        for n in name:
            n = ''.join(n.split())
            names.append(n)

        # print(polls)
        # print(names)
        item = GameItem()
        for name,poll in zip(names,polls):
            item['name']=name
            item['poll']=poll

            if response.url.find('list-0-0-0-0-0-0-0-0-0-0-1.html')!=-1:
                item['type']='网游'


            elif response.url.find('list-0-0-4-0-0-0-0-0-0-0-1.html')!=-1:

                item['type']='手游'


            else:
                item['type']='VR'

            yield item

 

 


#创建item对象类

items.py

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class GameItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    poll = scrapy.Field()
    type = scrapy.Field()

 

 

 

 

#保存文件

pipelines.py

 

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json

class Demo1Pipeline(object):
    def open_spider(self,spider):
        self.file = open('game.txt','a',encoding='utf-8')
    def process_item(self, item, spider):
        # if item['type']=='网游':
        #     with open('wangyou.txt','w',encoding='utf-8')
        self.file.write(json.dumps(dict(item),ensure_ascii=False)+'\n')


        print(item)
        self.file.flush()

        return item


    def close_spider(self,spider):
        self.file.close()
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值