【问题标题】:Scraping IMDB.com with beautifulsoup in python but can't get href from movie link用python中的beautifulsoup抓取IMDB.com,但无法从电影链接中获取href
【发布时间】:2017-03-23 11:17:15
【问题描述】:

我正在尝试获取电影的 href 链接(例如:在 IMDB 上搜索钢铁侠),但我似乎无法获取。运行代码时,我不断收到“无”,但如果我删除 .get('href'),代码将返回整行 html(包括我想要的链接)。我很感激这方面的任何帮助。谢谢!

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').get('href')


    return next_page


next_page_url = scrape_find_next_page(url)

【问题讨论】:

  • 看起来您获得的是 <td> 元素的 href 而不是链接
  • 我在任何地方都只看到过这个for link in soup.findAll("a"): print link.get("href")

标签: python html beautifulsoup href imdb


【解决方案1】:

您正在尝试从td 获取href,该属性不存在。您需要获取包含href 属性的a 标记

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多