成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

爬取豆瓣電影top250提取電影分類進行數(shù)據(jù)分析

Mertens / 948人閱讀

摘要:標(biāo)簽空格分隔爬蟲一爬取網(wǎng)頁,獲取需要內(nèi)容我們今天要爬取的是豆瓣電影頁面如下所示我們需要的是里面的電影分類,通過查看源代碼觀察可以分析出我們需要的東西。

標(biāo)簽(空格分隔):python爬蟲

一、爬取網(wǎng)頁,獲取需要內(nèi)容

我們今天要爬取的是豆瓣電影top250
頁面如下所示:

我們需要的是里面的電影分類,通過查看源代碼觀察可以分析出我們需要的東西。直接進入主題吧!

知道我們需要的內(nèi)容在哪里了,接下來就使用我們python強大的request庫先獲取網(wǎng)頁內(nèi)容下來吧!獲取內(nèi)容后,再使用一個好用的lxml庫來分析網(wǎng)頁內(nèi)容,然后獲取我們的內(nèi)容就可以做下一步操作了。
先貼出使用request庫和lxml分析的代碼

    def get_page(i):
            url = "https://movie.douban.com/top250?start={}&filter=".format(i)
                
            html = requests.get(url).content.decode("utf-8")    # 使用request庫獲取網(wǎng)頁內(nèi)容
        
            selector = etree.HTML(html)    # 使用lxml庫提取內(nèi)容
            """
                通過觀察頁面就能發(fā)現(xiàn)內(nèi)容在
下的一部分 """ content = selector.xpath("http://div[@class="info"]/div[@class="bd"]/p/text()") print(content) for i in content[1::2]: print(str(i).strip().replace(" ", "")) # print(str(i).split("/")) i = str(i).split("/") i = i[len(i) - 1] key = i.strip().replace(" ", "").split(" ") # 這里的strip和replace的使用目的是去除空格和空行之類 print(key)

通過獲取下來的內(nèi)容我們發(fā)現(xiàn)一部電影的各項內(nèi)容都是用"/"分隔著,我們只需要提取電影分類中的東西,所以我們需要使用

i = str(i).split("/")

來把內(nèi)容分隔成幾項內(nèi)容,因為電影分類排在最后,所以我們通過

i = i[len(i) - 1]

來獲取分隔后的最后一項也就是我們需要的電影分類,還有最后一步我們需要完成的,因為一部電影里面一般都有多個電影分類的標(biāo)簽,所以我們還要繼續(xù)分隔獲取到的電影分類,并且觀察可以知道電影分類之間只是用一個空格隔開,所以我們使用下面一行代碼就可以分離出各個分類:

key = i.strip().replace("
", "").split(" ")
二、接下來就是保存到mysql數(shù)據(jù)庫

把電影分類保存在mysql數(shù)據(jù)庫以便下面進行數(shù)據(jù)分析,這里我們使用到pymysql來連接mysql數(shù)據(jù)庫,首先我們需要在mysql數(shù)據(jù)庫建好表:

然后我們通過pymysql把數(shù)據(jù)保存到數(shù)據(jù)庫中,代碼如下:
首先要連接數(shù)據(jù)庫:

# 連接mysql數(shù)據(jù)庫
conn = pymysql.connect(host = "localhost", user = "root", passwd = "2014081029", db = "mysql", charset = "utf8")? # user為數(shù)據(jù)庫的名字,passwd為數(shù)據(jù)庫的密碼,一般把要把字符集定義為utf8,不然存入數(shù)據(jù)庫容易遇到編碼問題
cur = conn.cursor()? # 獲取操作游標(biāo)
cur.execute("use douban")? # 使用douban這個數(shù)據(jù)庫

在保存到數(shù)據(jù)庫之前,我們還有一個需要做得,那就是把250部電影的分類匯總數(shù)量,所以我們定義了一個字典來統(tǒng)計電影分類的個數(shù),這里的代碼是get_page函數(shù)的一部分,代碼如下:

    for i in content[1::2]:
        print(str(i).strip().replace("

", ""))
        # print(str(i).split("/"))
        i = str(i).split("/")
        i = i[len(i) - 1]
        key = i.strip().replace("
", "").split(" ")
        print(key)
        for i in key:
            if i not in douban.keys():
                douban[i] = 1
            else:
                douban[i] += 1

然后定義一個保存函數(shù),執(zhí)行插入操作,如果出現(xiàn)插入失敗,就執(zhí)行回滾操作,還有記得在操作完成之后,使用conn.close()和cur.close()來關(guān)閉數(shù)據(jù)庫連接,代碼如下:

    def save_mysql(douban):
        print(douban)  # douban在主函數(shù)中定義的字典
        for key in douban:
            print(key)
            print(douban[key])
            if key != "":
                try:
                    sql = "insert douban(類別, 數(shù)量) value(" + """ + key + ""," + """ + str(douban[key]) + """ + ");"
                    cur.execute(sql)
                    conn.commit()
                except:
                    print("插入失敗")
                    conn.rollback()
三、使用matplotlib進行數(shù)據(jù)可視化操作

首先,從數(shù)據(jù)庫中把電影分類和每個分類的數(shù)量分別存入一個列表中,然后使用matplotlib進行可視化操作,具體如下:

    def pylot_show():
        sql = "select * from douban;"  
        cur.execute(sql)
        rows = cur.fetchall()   # 把表中所有字段讀取出來
        count = []   # 每個分類的數(shù)量
        category = []  # 分類
    
        for row in rows:
            count.append(int(row[2]))   
            category.append(row[1])
    
        y_pos = np.arange(len(category))    # 定義y軸坐標(biāo)數(shù)
        plt.barh(y_pos, count, align="center", alpha=0.4)  # alpha圖表的填充不透明度(0~1)之間
        plt.yticks(y_pos, category)  # 在y軸上做分類名的標(biāo)記
    
        for count, y_pos in zip(count, y_pos):
            # 分類個數(shù)在圖中顯示的位置,就是那些數(shù)字在柱狀圖尾部顯示的數(shù)字
            plt.text(count, y_pos, count,  horizontalalignment="center", verticalalignment="center", weight="bold")  
        plt.ylim(+28.0, -1.0) # 可視化范圍,相當(dāng)于規(guī)定y軸范圍
        plt.title(u"豆瓣電影250")   # 圖表的標(biāo)題
        plt.ylabel(u"電影分類")     # 圖表y軸的標(biāo)記
        plt.subplots_adjust(bottom = 0.15) 
        plt.xlabel(u"分類出現(xiàn)次數(shù)")  # 圖表x軸的標(biāo)記
        plt.savefig("douban.png")   # 保存圖片

下面說明一下matplotlib的一些簡單使用,首先我們要導(dǎo)入matplotlib和numpy的包

import numpy as np
import matplotlib.pyplot as plt

這次可視化是柱狀圖,這里給出brah()函數(shù)的定義:

barh()
主要功能:做一個橫向條形圖,橫向條的矩形大小為: left, left + width, bottom, bottom + height
參數(shù):barh ( bottom , width , height =0.8, left =0, **kwargs )
返回類型:一個 class 類別, matplotlib.patches.Rectangle**實例
參數(shù)說明:

bottom: Bars 的垂直位置的底部邊緣

width: Bars 的長度
可選參數(shù):

height: bars 的高度

left: bars 左邊緣 x 軸坐標(biāo)值

color: bars 顏色

edgecolor: bars 邊緣顏色

linewidth: bar 邊緣寬度;None 表示默認(rèn)寬度;0 表示不 i 繪制邊緣

xerr: 若不為 None,將在 bar 圖上生成 errobars

yerr: 若不為 None,將在 bar 圖上生成 errobars

ecolor: 指定 errorbar 顏色

capsize: 指定 errorbar 的頂部(cap)長度

align: ‘edge’ (默認(rèn)) | ‘center’:‘edge’以底部為準(zhǔn)對齊;‘center’以 y 軸作為中心

log: [False|True] False (默認(rèn)),若為 True,使用 log 坐標(biāo)

然后就可以顯示出圖片來了

源碼在這里:
# -*- coding: utf-8 -*-
# !/usr/bin/env python

from lxml import etree
import requests
import pymysql
import matplotlib.pyplot as plt
from pylab import *
import numpy as np

# 連接mysql數(shù)據(jù)庫
conn = pymysql.connect(host = "localhost", user = "root", passwd = "2014081029", db = "mysql", charset = "utf8")
cur = conn.cursor()
cur.execute("use douban")

def get_page(i):
    url = "https://movie.douban.com/top250?start={}&filter=".format(i)

    html = requests.get(url).content.decode("utf-8")

    selector = etree.HTML(html)

    content = selector.xpath("http://div[@class="info"]/div[@class="bd"]/p/text()")
    print(content)

    for i in content[1::2]:
        print(str(i).strip().replace("

", ""))
        # print(str(i).split("/"))
        i = str(i).split("/")
        i = i[len(i) - 1]
        # print("zhe" +i)
        # print(i.strip())
        # print(i.strip().split(" "))
        key = i.strip().replace("
", "").split(" ")
        print(key)
        for i in key:
            if i not in douban.keys():
                douban[i] = 1
            else:
                douban[i] += 1

def save_mysql():
    print(douban)
    for key in douban:
        print(key)
        print(douban[key])
        if key != "":
            try:
                sql = "insert douban(類別, 數(shù)量) value(" + """ + key + ""," + """ + str(douban[key]) + """ + ");"
                cur.execute(sql)
                conn.commit()
            except:
                print("插入失敗")
                conn.rollback()


def pylot_show():
    sql = "select * from douban;"
    cur.execute(sql)
    rows = cur.fetchall()
    count = []
    category = []

    for row in rows:
        count.append(int(row[2]))
        category.append(row[1])
    print(count)
    y_pos = np.arange(len(category))
    print(y_pos)
    print(category)
    colors = np.random.rand(len(count))
    # plt.barh()
    plt.barh(y_pos, count, align="center", alpha=0.4)
    plt.yticks(y_pos, category)
    for count, y_pos in zip(count, y_pos):
        plt.text(count, y_pos, count,  horizontalalignment="center", verticalalignment="center", weight="bold")
    plt.ylim(+28.0, -1.0)
    plt.title(u"豆瓣電影250")
    plt.ylabel(u"電影分類")
    plt.subplots_adjust(bottom = 0.15)
    plt.xlabel(u"分類出現(xiàn)次數(shù)")
    plt.savefig("douban.png")


if __name__ == "__main__":
    douban = {}
    for i in range(0, 250, 25):
        get_page(i)
    # save_mysql()
    pylot_show()
    cur.close()
    conn.close()

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/38061.html

相關(guān)文章

  • (轉(zhuǎn))Python3爬取豆瓣電影保存到MySQL數(shù)據(jù)

    摘要:行代碼實現(xiàn)爬取豆瓣電影排行榜代碼基于,用到的類庫有標(biāo)題文字通過偽造請求頭或設(shè)置代理等方式獲取頁面內(nèi)容,參考文檔對頁面進行解析,提取數(shù)據(jù),參考文檔版本中用于操作數(shù)據(jù)庫,中則使用,安裝用到的幾個類庫分析豆瓣電影頁面頁面分析爬取數(shù)據(jù)之前,我們都需 48行代碼實現(xiàn)Python3爬取豆瓣電影排行榜代碼基于python3,用到的類庫有: 標(biāo)題文字 requests:通過偽造請求頭或設(shè)置代理等方式獲...

    nevermind 評論0 收藏0
  • scrapy爬取豆瓣Top250電影

    摘要:這次我們爬取的內(nèi)容準(zhǔn)備步驟找到格式網(wǎng)頁中需要爬取的數(shù)據(jù)的例如我們需要爬取圖片的這里用的是不會用的同學(xué)請百度然后我們開始建立工程打開然后在你想要建立工程的目錄下面輸入就會自動建立一個工程然后去根目錄建立一個去這個目錄里建立一個注意這里的主爬蟲 這次我們爬取的內(nèi)容 showImg(https://segmentfault.com/img/bVSirX?w=1021&h=521); 準(zhǔn)備步驟...

    codergarden 評論0 收藏0
  • 80行代碼爬取豆瓣Top250電影信息并導(dǎo)出到csv及數(shù)據(jù)

    摘要:查看源碼下載頁面并處理提取數(shù)據(jù)觀察該網(wǎng)站結(jié)構(gòu)可知該頁面下所有電影包含在標(biāo)簽下。使用語句獲取該標(biāo)簽在標(biāo)簽中遍歷每個標(biāo)簽獲取單個電影的信息。以電影名字為例清洗數(shù)據(jù)其余部分詳見源碼頁面跳轉(zhuǎn)檢查后頁標(biāo)簽。 查看源碼 1 下載頁面并處理 DOWNLOAD_URL = http://movie.douban.com/top250/ html = requests.get(url).text tr...

    galaxy_robot 評論0 收藏0
  • scrapy入門:豆瓣電影top250爬取

    摘要:本文內(nèi)容爬取豆瓣電影頁面內(nèi)容,字段包含排名,片名,導(dǎo)演,一句話描述有的為空,評分,評價人數(shù),上映時間,上映國家,類別抓取數(shù)據(jù)存儲介紹爬蟲框架教程一入門創(chuàng)建項目創(chuàng)建爬蟲注意,爬蟲名不能和項目名一樣應(yīng)對反爬策略的配置打開文件,將修改為。 本文內(nèi)容 爬取豆瓣電影Top250頁面內(nèi)容,字段包含:排名,片名,導(dǎo)演,一句話描述 有的為空,評分,評價人數(shù),上映時間,上映國家,類別 抓取數(shù)據(jù)存儲 ...

    xialong 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<