ウェブスクレーピング[パート1 ]


アリアドニル / スクラングネスIMDB


映画タイトル、年、評価、ジャンル、票を落とす


おい、皆さん.
あなたはPythonでは、HTMLとPythonの基本だけでは、このウェブを削るのチュートリアルでは十分である必要があります.

…に飛び込みましょう


使用するツールは以下の通りです.

  • リクエストは、私たちがHTMLファイルを得るためにHTTPリクエストを送るのを許します

  • BeautiFootSpyは、HTMLファイルを解析するのに役立ちます

  • パンダは、データをきれいにして分析するためにデータをアセンブルするのを助けます

  • CSVファイル形式でデータを共有する場合
  • 始めましょう。


    このチュートリアルでは、我々はscrapeIMDB ウェブサイト、我々は、タイトル、年、評価、ジャンルなどを得ることができます.
    最初に、我々は我々のスクレーパーを構築するためにツールをインポートします
    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    import csv
    
    Webページの内容を結果変数に取得する
    url = "https://www.imdb.com/search/title/?groups=top_1000"
    results = requests.get(url)
    
    コンテンツを理解しやすくするために、我々はBeautiFookieを使用しているコンテンツはスープの変数に格納されて
    soup = BeautifulSoup(results.text, "html.parser")
    
    そしてデータを保存するリストを初期化する
    titles = []        #Stores the title of movie
    years = []         #Stores the launch year of the movie
    time = []          #Stores movie duration
    imdb_ratings = []  #Stores the rating of the movie
    genre = []         #Stores details regarding genre of the movie
    votes = []         #Store the no.of votes for the movie
    
    今、それを検査することによって右の映画コンテナーを見つけて、以下のイメージのように見える映画divの上にホバー

    そして、私たちはクラス名で50 divを見ることができます:lister-item mode-advancedしたがって、すべてのdivのクラス名を
    movie_div = soup.find_all("div", class_="lister-item mode-advanced")
    
    すべての属性はクラスを持つすべてのdivのものを抽出する
    リネームアイテムモード
    今、それぞれに入るlister-item mode-advanced divとタイトル、年、評価、ジャンル、映画の期間を取得

    それで、我々はタイトル、年、評価などを得るためにあらゆるdivを繰り返します.
    for movieSection in movie_div:
    

    タイトル抽出



    イメージから、我々は映画名がdiv > H 3 > Aの下に置かれるのを見ることができます
    name = movieSection.h3.a.text  #we're iterating those divs using <b>movieSection<b> variable
    titles.append(name) #appending the movie names into <b>titles</b> list  
    

    抽出年



    イメージから、私たちは、映画開始年がdiv > h 3 > span(クラス名=「lister item year」)の下に置かれるのを見ることができます
    year = movieSection.h3.find("span", class_="lister-item-year").text
    years.append(year)   #appending into years list
    
    同様に、我々は評価、ジャンル、MovieDuration
    ratings = movieSection.strong.text
    imdb_ratings.append(ratings)   #appending ratings into list
    category = movieSection.find("span", class_="genre").text.strip()
    genre.append(category)         #appending category into Genre list
    runTime = movieSection.find("span", class_="runtime").text
    time.append(runTime)           #appending runTime into time list
    

    票の抽出



    イメージと同様に、私たちはクラス名=「NV」で2つのスパンタグを持っているのを見ることができます.それで、投票のために、我々はNV[0]を考慮する必要があります、そして、グロスコレクションNv[1]のために
    nv = movieSection.find_all("span", attrs={"name": "nv"})
    vote = nv[0].text
    votes.append(vote)
    
    今、パンダでデータフレームを構築します
    データを保存するためには、テーブルにきちんと作成しなければならないので、本当に理解できます
    そして、我々はそれをすることができます..
    movies = pd.DataFrame(
        {
            "Movie": titles,
            "Year": years,
            "RunTime": time,
            "imdb": imdb_ratings,
            "Genre": genre,
            "votes": votes,
        }
    )
    
    そして今、データフレームを印刷しましょう

    私たちが列16と25の上で見ることができるように、データの矛盾があります.それで、我々はきれいにする必要があります
     movies["Year"] = movies["Year"].str.extract("(\\d+)").astype(int) #Extracting only numerical values. so we can commit "I"
     movies["RunTime"] = movies["RunTime"].str.replace("min", "minutes") #replacing <b>min</b> with <b>minutes</b>
     movies["votes"] = movies["votes"].str.replace(",", "").astype(int) #removing "," to make it more clear
    
    そして、掃除の後、我々は見ます
    print(movies)
    

    また、データをエクスポートすることができます.CSVファイル形式.
    エクスポートするには
    ファイルを作成する.ファイル拡張子
    movies.to_csv(r"C:\Users\Aleti Sunil\Downloads\movies.csv", index=False, header=True)
    

    あなたは私の最後のコードを得ることができますGithub repo
    次のパートでは、このIMDBリストのすべてのページをループする方法を説明します
    希望が役に立つ
    エー❤️ 素晴らしいです😊

    Happycoding