0

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I think name.text makes more sense. name.text[0] would print the 1st letter of the name.
I did both, print(name.text[0]) prints me nothing and name.text prints me all the titles and I just one the first
Now I used name.text[1] and it prints the first letter of each title :/
What is your expected output? Is it the title only?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.