2016-11-09 4 views
2

私は特定のタイトルを検索するためにIMDBを掻き集めようとしています。検索結果に最初のリンクを入力してから、映画がリリースされた年.find()に入れるべきhtmlの部分を把握できないようです。Pythonでbeautfulsoupを使ってIMDBをスクラップする。検索結果を入力してから年を入力してください

最初の機能は元のURLを処理して収集し、URLの新しい2番目の部分(ムービーページ用)に結合します。

ご協力いただきありがとうございました。

from bs4 import BeautifulSoup 
import requests 
from urllib.parse import urljoin # For joining next page url with base url 

search_terms = input("What movie do you want to know about?\n> ").split() 

url = "http://www.imdb.com/find?ref_=nv_sr_fn&q=" + '+'.join(search_terms) + '&s=all' 

def scrape_find_next_page(url): 
    headers = {'User-Agent': 'Mozilla/5.0'} 
    response = requests.get(url, headers=headers) 
    soup = BeautifulSoup(response.text, "html.parser") 

    next_page = soup.find('td', 'result_text').find('a').get('href') 

    return next_page 


next_page_url = scrape_find_next_page(url) 

new_page = urljoin(url, next_page_url) 



def scrape_movie_data(next_page_url): 
    headers = {'User-Agent': 'Mozilla/5.0'} 
    response = requests.get(url, headers=headers) 
    soup = BeautifulSoup(response.text, "html.parser") 

    title_year = soup.find('span','titleYear').find('a').get_text() 

    return title_year 

print(scrape_movie_data(new_page)) 
+0

使用DevTool(「ページdoesnの場合JavaScriptを使用してdatを読み込むa)。 – furas

答えて

2

まず問題:scrape_movie_data(next_page_url)にあなたはあなたが間違ったページを読んurl代わりのrequests.get()next_page_urlを使用しています。

response = requests.get(next_page_url, headers=headers) 

第二の問題:

def scrape_movie_data(next_page_url): 
    headers = {'User-Agent': 'Mozilla/5.0'} 
    response = requests.get(next_page_url, headers=headers) 
    soup = BeautifulSoup(response.text, "html.parser") 

    title_year = soup.find('span', {'id': 'titleYear'}).find('a').get_text() 

    return title_year 

EDIT:グーグルでチェックIMDB APIあなたは{'id': 'titleYear'}

title_year = soup.find('span', {'id': 'titleYear'}).find('a').get_text() 

find()の最終バージョンを使用する必要があります。いくつかの興味深い結果

SO - IMDB API to retrieve character information

SO - Does IMDB provide an API?

、あなたがこすりする必要はありませんので、あなたはJSONとして結果を得ることができます。

他のポータル:

OMDb API -The Open Movie Database

The Movie DB API


EDIT: JSONデータ要素を見つけるためにクローム/ Firefoxで

import requests 

url = 'http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q={}' 
#url = 'http://www.imdb.com/xml/find?json=1&nr=1&nm=on&q={}' 

headers = {'User-Agent': 'Mozilla/5.0'} 

title = input("Title: ").split() 

response = requests.get(url.format(title[0]), headers=headers) 

data = response.json() 

for x in data['title_popular']: # data['title_approx']: 
    print('title:', x['title']) 
    print(' year:', x['title_description'][:4]) 
    print('---') 
    print(' id:', x['id']) 
    print('name:', x['name']) 
    print('  title:', x['title']) 
    print('episode_title:', x['episode_title']) 
    print('title_description:', x['title_description']) 
    print('  description:', x['description']) 
    print('------------------------------------') 
+0

APIに関するリンクを追加します。 – furas

関連する問題