【问题标题】:Scraping IMDB with beautfulsoup in Python; search result then enter link then get year在 Python 中使用 beautifulsoup 抓取 IMDB;搜索结果然后输入链接然后获取年份
【发布时间】:2017-03-23 19:52:37
【问题描述】:

我正在尝试抓取 IMDB 以搜索特定标题,在搜索结果中输入第一个链接,然后打印电影发行的年份(以及后来的其他信息),但我似乎无法弄清楚将 html 的哪一部分放入 .find()。

第一个函数起作用并收集原始 url 并将其与 url 的新第二部分(用于电影页面)连接。

感谢您的帮助,这几天卡住了!

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))

【问题讨论】:

  • 在 Chrome/Firefox 中使用 DevTool 来查找元素(如果页面不使用 JavaScript 加载数据)。

标签: python web-scraping beautifulsoup gettext imdb


【解决方案1】:

第一个问题:在scrape_movie_data(next_page_url) 中,您在requests.get() 中使用url 而不是next_page_url,所以您读错了页面。

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

第二个问题:你必须在find()中使用{'id': 'titleYear'}

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

最终版本:

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

编辑: 在 Google 中查看 IMDB API。一些有趣的结果

SO - IMDB API to retrieve character information

SO - Does IMDB provide an API?

你可以得到 JSON 格式的结果,所以你不必刮。

其他门户:

OMDb API -The Open Movie Database

The Movie DB API


编辑: JSON 数据

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('------------------------------------')

【讨论】:

  • 我添加了关于 API 的链接。
猜你喜欢
  • 2021-01-21
  • 2021-06-22
  • 2018-03-21
  • 2016-01-28
  • 2015-11-23
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多