Vinicio López
Vinicio López

Reputation: 25

TypeError: 'NoneType' object is not subscriptable, webscrapin Python

This code is to search a movie from a webpage and print the first title of the search result.

from urllib.request import urlopen
import urllib
from bs4 import BeautifulSoup
import requests
import pprint

def infopelicula(nombrepelicula):
    my_url='http://www.imdb.com/find?ref_=nv_sr_fn&q='+nombrepelicula+'&s=tt'
    rprincipal = requests.get(my_url)
    soup= BeautifulSoup(rprincipal.content, 'html.parser')
    title = soup.findAll("td", class_="result_text")
    for name in title:
        titulo = name.parent.find("a", href=True)
        print (name.text)[0]

It does work but when print the title, the Error appears. Here an example:

>>>infopelicula("Harry Potter Chamber")
Harry Potter and the Chamber of Secrets (2002) 
Traceback (most recent call last):File "<pyshell#49>", line 1, in <module>
infopelicula("Harry Potter Chamber")
File "xxxx", line 14, in infopelicula print (name.text)[0]
TypeError: 'NoneType' object is not subscriptable

Upvotes: 0

Views: 2289

Answers (2)

SIM
SIM

Reputation: 22440

How about this one:

import requests
from bs4 import BeautifulSoup

def infopelicula():
    my_url = 'http://www.imdb.com/find?ref_=nv_sr_fn&q="Harry Potter Chamber"&s=tt'
    soup = BeautifulSoup(requests.get(my_url).text, 'lxml')
    for name in soup.find_all("td",class_="result_text"):
        title = name.find_all("a",text=True)[0]
        print (title.text)
infopelicula()

Partial output:

Harry Potter and the Sorcerer's Stone
Harry Potter and the Goblet of Fire
Harry Potter and the Half-Blood Prince
Harry Potter and the Deathly Hallows: Part 2

For the first title only:

import requests
from bs4 import BeautifulSoup

def infopelicula():
    my_url = 'http://www.imdb.com/find?ref_=nv_sr_fn&q="Harry Potter Chamber"&s=tt'
    soup = BeautifulSoup(requests.get(my_url).text, 'lxml')
    for name in soup.find_all("td",class_="result_text")[:1]:
        title = name.find_all("a",text=True)[0]
        print (title.text)
infopelicula()

Output:

Harry Potter and the Chamber of Secrets

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49803

In Python3.5, print is a function that returns None, which (as the error clearly says) can't be subscripted.

Maybe you meant print(name.text[0])?

Upvotes: 2

Related Questions