大數(shù)據(jù) - 創(chuàng)造101 - 數(shù)據(jù)整理

數(shù)據(jù)源

1旅东、小紅書
2抵代、百度百科-創(chuàng)造101
3、騰訊管網(wǎng)

101changtui.jpg

采集分析

這次有100多人數(shù)據(jù)需要采集康吵,而且分主副頁面晦嵌,必須使用爬蟲處理了惭载。一個人一天20個棕兼,5天也能干完。
爬蟲打算使用scrapy茎芋,文檔多田弥,使用方便偷厦,支持xpath和css語法只泼,再加上正則表達式请唱,基本上除了有反爬蟲設(shè)置十绑,否則沒什么網(wǎng)頁處理不了扳躬。
主要目標如下:
1坦报、人名
2、公司
3潜的、排名
4信不、身高
5抽活、體重
6下硕、英文名
7梭姓、圖片
8誉尖、數(shù)據(jù)轉(zhuǎn)存json和csv格式處理

項目開始

項目環(huán)境
1琢感、python 3.6.3
2猩谊、scrapy 1.5.0
3牌捷、WIN10

創(chuàng)建項目

pip install scrapy
pip install xpinyin
scrapy startproject P101

編寫爬蟲

修改spiders下的P101.py暗甥,分兩段分別編寫
1、分析主頁面
進入命令行模式寄月,分析需要數(shù)據(jù)

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

2漾肮、分析子頁面
進入命令行模式克懊,分析需要數(shù)據(jù),EP1~EP10數(shù)據(jù)

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):  # 將戰(zhàn)隊ID號取出扮念,構(gòu)建完整的戰(zhàn)隊詳情頁的URL柜与,并使用parse_team函數(shù)解析
        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

                # 構(gòu)造隊員信息URL伞鲫,回調(diào)函數(shù)為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

優(yōu)化爬蟲

1儒搭、圖片名稱是什么搂鲫?
網(wǎng)站圖片是puui.qpic.cn/media_img/0/null1524465427/0魂仍,這是什么鬼擦酌?
獲取下來需要修改成我們能找到的圖片。打算使用名字拼音作為圖片名字赶诊。

2寓调、需要使用ImagesPipeline技術(shù)下載圖片捶牢,當然如果你覺得麻煩巍耗,直接request也可以。
沒太多難度驯耻,網(wǎng)上找個教程霎迫,添加進來就能用了知给。

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系統(tǒng)非法文件夾名字的字符串
    """
    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系統(tǒng)的文件夾非法字符,避免無法創(chuàng)建目錄
        :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

數(shù)據(jù)存儲

存成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

拿到的數(shù)據(jù)

{"name": "\u9a6c\u5174\u94b0", "engname": "maxingyu", "starid": "1661557", "pic": "http://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": "http://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": "http://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": "http://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": "http://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": "http://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"}

結(jié)束語

爬蟲是數(shù)據(jù)采集最基礎(chǔ)的技術(shù),大數(shù)據(jù)必備知識之一况木。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末火惊,一起剝皮案震驚了整個濱河市屹耐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌犯眠,老刑警劉巖筐咧,帶你破解...
    沈念sama閱讀 222,865評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摩疑,死亡現(xiàn)場離奇詭異雷袋,居然都是意外死亡楷怒,警方通過查閱死者的電腦和手機鸠删,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來碉怔,“玉大人撮胧,你說我怎么就攤上這事芹啥∧够常” “怎么了疏虫?”我有些...
    開封第一講書人閱讀 169,631評論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長官扣。 經(jīng)常有香客問我惕蹄,道長遭顶,這世上最難降的妖魔是什么棒旗? 我笑而不...
    開封第一講書人閱讀 60,199評論 1 300
  • 正文 為了忘掉前任铣揉,我火速辦了婚禮逛拱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘饱狂。我一直安慰自己嗡官,他們只是感情好,可當我...
    茶點故事閱讀 69,196評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著婆咸,像睡著了一般块差。 火紅的嫁衣襯著肌膚如雪憨闰。 梳的紋絲不亂的頭發(fā)上鹉动,一...
    開封第一講書人閱讀 52,793評論 1 314
  • 那天泽示,我揣著相機與錄音械筛,去河邊找鬼埋哟。 笑死定欧,一個胖子當著我的面吹牛砍鸠,可吹牛的內(nèi)容都是我干的爷辱。 我是一名探鬼主播饭弓,決...
    沈念sama閱讀 41,221評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼咏花,長吁一口氣:“原來是場噩夢啊……” “哼昏翰!你這毒婦竟也來了棚菊?” 一聲冷哼從身側(cè)響起统求,我...
    開封第一講書人閱讀 40,174評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冒滩,沒想到半個月后开睡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體篇恒,經(jīng)...
    沈念sama閱讀 46,699評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡胁艰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,770評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了解虱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片殴泰。...
    茶點故事閱讀 40,918評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖至会,靈堂內(nèi)的尸體忽然破棺而出奉件,到底是詐尸還是另有隱情,我是刑警寧澤糖埋,帶...
    沈念sama閱讀 36,573評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站祟敛,受9級特大地震影響馆铁,放射性物質(zhì)發(fā)生泄漏埠巨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,255評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望脱衙。 院中可真熱鬧捐韩,春花似錦荤胁、人聲如沸寨蹋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秸讹。三九已至璃诀,卻和暖如春劣欢,著一層夾襖步出監(jiān)牢的瞬間凿将,已是汗流浹背牧抵。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評論 1 274
  • 我被黑心中介騙來泰國打工妹孙, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留涕蜂,地道東北人。 一個月前我還...
    沈念sama閱讀 49,364評論 3 379
  • 正文 我出身青樓萨西,卻偏偏與公主長得像谎脯,于是被迫代替她去往敵國和親源梭。 傳聞我的和親對象是個殘疾皇子废麻,可洞房花燭夜當晚...
    茶點故事閱讀 45,926評論 2 361

推薦閱讀更多精彩內(nèi)容