0
此代码是从网页搜索电影并打印搜索结果的第一个标题。TypeError:'NoneType'对象不可下标,网页扫描Python
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]
它可以工作,但打印标题时出现错误。 这里一个例子:
>>>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
我认为'name.text'更有意义。 'name.text [0]'会打印名字的第一个字母。 – MrE
我做了两个,打印(name.text [0])打印我没有和name.text打印我所有的标题,我只是第一个 –
现在我用name.text [1],它打印每个的第一个字母title:/ –