大数据 - 创造101 - 数据整理

数据源

1、小红书
2、百度百科-创造101
3、腾讯管网

101changtui.jpg

采集分析

这次有100多人数据需要采集,而且分主副页面,必须使用爬虫处理了。一个人一天20个,5天也能干完。
爬虫打算使用scrapy,文档多,使用方便,支持xpath和css语法,再加上正则表达式,基本上除了有反爬虫设置,否则没什么网页处理不了。
主要目标如下:
1、人名
2、公司
3、排名
4、身高
5、体重
6、英文名
7、图片
8、数据转存json和csv格式处理

项目开始

项目环境
1、python 3.6.3
2、scrapy 1.5.0
3、WIN10

创建项目

pip install scrapy
pip install xpinyin
scrapy startproject P101

编写爬虫

修改spiders下的P101.py,分两段分别编写
1、分析主页面
进入命令行模式,分析需要数据

scrapy shell http://v.qq.com/biu/101_star_web
>>>sel.xpath('//div[@class="list_item"]//a[contains(@href, "javascript:;")]/text()')
陈意涵
....
....
>>>

2、分析子页面
进入命令行模式,分析需要数据,EP1~EP10数据

scrapy shell http://v.qq.com/doki/star?id=1661556
>>>sel.xpath('//div[@id="101"]/@data-round').extract()
[',20,26,31,35,37,35,25,,']
>>>

3、爬虫源码

# -*- coding: utf-8 -*-
from scrapy import Spider, Request
from P101.items import DokiSlimItem
from P101.items import DokiItem
import json
from xpinyin import Pinyin
import urllib


class P101Spider(Spider):
    name = 'P101'
    allowed_domains = ['v.qq.com']
    start_urls = ['http://v.qq.com/biu/101_star_web']
    p101_url = 'http://v.qq.com/biu/101_star_web'
    # allowed_domains = ['127.0.0.1:8080']
    # p101_url = 'http://127.0.0.1:8080/rank.html'

    single_url = 'http://v.qq.com/doki/star?id={starid}'

    def start_requests(self):  # 将战队ID号取出,构建完整的战队详情页的URL,并使用parse_team函数解析
        yield Request(self.p101_url, self.parse_p101)

    def parse_p101(self, response):

        p = Pinyin()
        # sel.xpath('//div[@class="list_item"]//a[contains(@href, "javascript:;")]/text()')
        for divs in response.xpath('//div[@class="list_item"]'):
            item1 = DokiSlimItem()
            for name in divs.xpath('.//a[contains(@href, "javascript:;")]/text()'):
                print(name.extract())
                cnname = name.extract()
                engname = p.get_pinyin(cnname, '')
                item1['name'] = cnname
                item1['engname'] = engname
            for starid in divs.xpath('.//a[@class="pic"][contains(@href, "javascript:;")]/@data-starid'):
                print(starid.extract())
                item1['starid'] = starid.extract()
            for pic in divs.xpath('.//a[@class="pic"][contains(@href, "javascript:;")]/img/@src'):
                print(pic.extract())
                item1['pic'] = pic.extract()
                item1['images'] = engname + ".png"
                # strurl = urllib.parse.quote(pic.extract().replace('.', ''))
                # strurl = "http://127.0.0.1:8080"+strurl
                strurl = pic.extract()
                strurl = "http:"+strurl
                item1['image_urls'] = [strurl]
                yield item1

                # 构造队员信息URL,回调函数为parse_idol
                yield Request(self.single_url.format(starid=item1['starid']), self.parse_idol)

    def parse_idol(self, response):  # 将队员的信息存入Item
        p = Pinyin()
        item2 = DokiItem()
        starid = str(response.url).strip().split("id=")[-1]
        epsdata = response.xpath('//div[@id="101"]/@data-round').extract()
        item2["epsdata"] = epsdata[0]

        properties = response.xpath('//div[@class="wiki_info_1"]//div[@class="line"]')
        name = properties[0].xpath('.//span[@class="content"]/text()').extract()
        # item2["name"] = name[0]
        cnname = name[0]
        engname = p.get_pinyin(cnname, '')
        item2['name'] = cnname
        item2['engname'] = engname
        item2['starid'] = starid

        height = properties[5].xpath('.//span[@class="content"]/text()').extract()
        item2["height"] = height[0]
        weight = properties[6].xpath('.//span[@class="content"]/text()').extract()
        item2["weight"] = weight[0]
        hometown = properties[7].xpath('.//span[@class="content"]/text()').extract()
        item2["hometown"] = hometown[0]
        yield item2

优化爬虫

1、图片名称是什么?
网站图片是puui.qpic.cn/media_img/0/null1524465427/0,这是什么鬼?
获取下来需要修改成我们能找到的图片。打算使用名字拼音作为图片名字。

2、需要使用ImagesPipeline技术下载图片,当然如果你觉得麻烦,直接request也可以。
没太多难度,网上找个教程,添加进来就能用了。

3、源码

# -*- 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
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
from .items import DokiSlimItem
from scrapy import Request
from scrapy import log
import requests
import re
import logging
import json


def strip(path):
    """
    :param path: 需要清洗的文件夹名字
    :return: 清洗掉Windows系统非法文件夹名字的字符串
    """
    path = re.sub(r'[?\\*|“<>:/]', '', str(path))
    return path


class P101Pipeline(object):
    def process_item(self, item, spider):
        return item


class P101ImgDownloadPipeline(ImagesPipeline):
    default_headers = {
        'accept': 'image/webp,image/*,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, sdch, br',
        'accept-language': 'zh-CN,zh;q=0.8,en;q=0.6',
#         'referer': 'http://puui.qpic.cn/media_img/0/',
        'referer': 'http://127.0.0.1:8080/',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
    }

    def file_path(self, request, response=None, info=None):
        """
        :param request: 每一个图片下载管道请求
        :param response:
        :param info:
        :param strip :清洗Windows系统的文件夹非法字符,避免无法创建目录
        :return: 每套图的分类目录
        """
        print('abc:')
        item = request.meta['item']
        folder = item
        print('folder:', folder)
        folder_strip = strip(folder)
        filename = u'{0}'.format(folder_strip)
        return filename

    def get_media_requests(self, item, info):
        if isinstance(item, DokiSlimItem):
            logging.debug("get_media_requests:"+item['image_urls'][0])
            print('item:', item)
            for image_url in item['image_urls']:
                self.default_headers['referer'] = image_url
#                 yield Request(image_url, headers=self.default_headers)
                logging.debug("get_media_requests url:"+image_url)
    #             referer = item['UserIcon']
                print('url:', image_url)
                yield Request(image_url, meta={'item': item['images']})

#         for image_url in item['image_urls']:
#             self.default_headers['referer'] = image_url
#             print('xxxx:'+image_url)
#             yield Request(image_url, headers=self.default_headers)

    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        item['image_paths'] = image_paths
        return item


class JsonPipeline(object):

    def open_spider(self, spider):
        self.file = open('data.json', 'w')

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

    def process_item(self, item, spider):
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        return item

数据存储

存成json格式,写到pipeline文件里了

1、源码

# -*- 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
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
from .items import DokiSlimItem
from scrapy import Request
from scrapy import log
import requests
import re
import logging
import json


class JsonPipeline(object):

    def open_spider(self, spider):
        self.file = open('data.json', 'w')

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

    def process_item(self, item, spider):
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        return item

拿到的数据

{"name": "\u9a6c\u5174\u94b0", "engname": "maxingyu", "starid": "1661557", "pic": "//puui.qpic.cn/media_img/0/null1524465404/0", "images": "maxingyu.png", "image_urls": ["http://puui.qpic.cn/media_img/0/null1524465404/0"]}
{"name": "\u5218\u601d\u7ea4", "engname": "liusixian", "starid": "1642387", "pic": "//puui.qpic.cn/media_img/0/null1524465187/0", "images": "liusixian.png", "image_urls": ["http://puui.qpic.cn/media_img/0/null1524465187/0"]}
{"name": "\u5f20\u695a\u5bd2", "engname": "zhangchuhan", "starid": "1661544", "pic": "//puui.qpic.cn/media_img/0/null1524466277/0", "images": "zhangchuhan.png", "image_urls": ["http://puui.qpic.cn/media_img/0/null1524466277/0"]}
{"name": "\u5411\u4fde\u661f", "engname": "xiangyuxing", "starid": "1572221", "pic": "//puui.qpic.cn/media_img/0/null1524465963/0", "images": "xiangyuxing.png", "image_urls": ["http://puui.qpic.cn/media_img/0/null1524465963/0"]}
{"name": "\u5434\u831c", "engname": "wuqian", "starid": "1661559", "pic": "//puui.qpic.cn/media_img/0/null1524465836/0", "images": "wuqian.png", "image_urls": ["http://puui.qpic.cn/media_img/0/null1524465836/0"]}
{"name": "\u5c39\u854a", "engname": "yinrui", "starid": "1661563", "pic": "//puui.qpic.cn/media_img/0/null1524466237/0", "images": "yinrui.png", "image_urls": ["http://puui.qpic.cn/media_img/0/null1524466237/0"]}
{"epsdata": ",8,8,10,9,9,9,12,,", "name": "\u5085\u83c1", "engname": "fujing", "starid": "1661523", "height": "168", "weight": "46kg", "hometown": "\u4e0a\u6d77"}
{"epsdata": ",,94,90,55,36,23,2,,", "name": "\u738b\u83ca", "engname": "wangju", "starid": "1661570", "height": "165", "weight": "60kg", "hometown": "\u4e0a\u6d77"}
{"epsdata": ",14,16,17,29,25,26,26,,", "name": "\u5434\u6620\u9999", "engname": "wuyingxiang", "starid": "1512788", "height": "164", "weight": "64kg", "hometown": "\u5723\u4fdd\u7f57"}
{"epsdata": ",69,66,47,40,47,48,,,", "name": "\u52fe\u96ea\u83b9", "engname": "gouxueying", "starid": "1597083", "height": "164", "weight": "46kg", "hometown": "\u5317\u4eac"}
{"epsdata": ",42,43,42,48,50,51,,,", "name": "\u5f20\u6eaa", "engname": "zhangxi", "starid": "1661547", "height": "163", "weight": "45kg", "hometown": "\u6dc4\u535a"}
{"epsdata": ",45,50,39,53,54,58,,,", "name": "\u5c39\u854a", "engname": "yinrui", "starid": "1661563", "height": "166", "weight": "42kg", "hometown": "\u91cd\u5e86"}

结束语

爬虫是数据采集最基础的技术,大数据必备知识之一。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容