python随机爬取五个电影演员一生生涯中出演的电影名称

发布时间 2023-05-22 10:27:43作者: YE-

为了随机爬取电影演员的电影,需要使用Python中的网络爬虫技术和相关的第三方库,如requests和BeautifulSoup。以下是一个简单的示例程序:

import random
import requests
from bs4 import BeautifulSoup

# 输入要爬取的演员的姓名
actor_name = input('请输入要爬取的演员的姓名:')

# 构造搜索网址
url = 'https://www.imdb.com/find?q=' + actor_name.replace(' ', '+') + '&s=nm&exact=true'

# 发送网络请求
response = requests.get(url)

# 解析网页内容,提取演员页面链接
soup = BeautifulSoup(response.text, 'html.parser')
actor_link = soup.select('td.result_text > a')[0]['href']

# 访问演员页面链接
actor_url = 'https://www.imdb.com' + actor_link
response = requests.get(actor_url)

# 解析演员页面内容,提取所有电影标题
soup = BeautifulSoup(response.text, 'html.parser')
movie_titles = [x.text for x in soup.select('div.filmo-row > b > a')]

# 随机选择五个电影并输出
print('演员 %s 的随机电影清单:' % (actor_name))
for i in range(5):
    movie_title = random.choice(movie_titles)
    print(movie_title)

这个程序首先要求用户输入演员的姓名,然后构造IMDb搜索网址,并发送网络请求。接着,程序解析搜索结果,提取演员页面链接,再次访问该链接并解析演员页面内容,提取所有电影标题信息。随后,程序从电影标题列表中随机选择五个,并将它们输出到控制台。

需要注意的是,这个程序并没有考虑到一些细节问题,例如演员名字拼写错误、有多个搜索结果、演员没有电影作品,以及网页解析失败等情况。因此,在实际应用中,需要进行一些额外的异常处理和输入验证。

爬取的时候,可能出现搜索的结果不显示,有以下原因:

1.确保输入的演员姓名准确无误,包括拼写、大小写、空格等。最好直接复制粘贴演员的全名。

2.尝试使用英文名字或原名进行搜索,例如使用"Tom Hanks"而不是"汤姆·汉克斯"。

3.修改程序中的搜索网址和选择器,以适应IMDb网站的变化。查看IMDb网站的HTML源代码,找到演员页面链接所在的位置,并将选择器修改为对应的CSS选择器。

4.检查爬虫程序是否被防火墙或反爬虫机制所阻挡,可以尝试使用其他网络代理或用户代理头信息进行访问。

要将上述代码进行可视化处理,可以使用Python中的GUI框架,如Tkinter或PyQt。以下是一个使用Tkinter实现的简单演示程序:

import random
import requests
from bs4 import BeautifulSoup
import tkinter as tk

# GUI界面
root = tk.Tk()
root.title('随机电影爬取器')
root.geometry('400x300')

# 创建GUI控件
label = tk.Label(root, text='请输入要随机爬取电影的演员姓名:')
label.pack(pady=10)

entry = tk.Entry(root, width=30, font=('Arial', 14))
entry.pack(pady=10)

button = tk.Button(root, text='搜索', font=('Arial', 14), command=lambda: search_movies(entry.get()))
button.pack(pady=10)

movies_text = tk.Text(root, width=50, height=15, font=('Arial', 12))
movies_text.pack(pady=10)

# 搜索函数
def search_movies(actor_name):
    # 构造搜索网址
    url = 'https://www.imdb.com/find?q=' + actor_name.replace(' ', '+') + '&s=nm&exact=true'

    # 发送网络请求
    response = requests.get(url)

    # 解析网页内容,提取演员页面链接
    soup = BeautifulSoup(response.text, 'html.parser')
    try:
        actor_link = soup.select('td.result_text > a')[0]['href']
    except:
        movies_text.delete('1.0', 'end')
        movies_text.insert('end', '未找到演员信息,请重新尝试。')
        return

    # 访问演员页面链接
    actor_url = 'https://www.imdb.com' + actor_link
    response = requests.get(actor_url)

    # 解析演员页面内容,提取所有电影标题
    soup = BeautifulSoup(response.text, 'html.parser')
    movie_titles = [x.text for x in soup.select('div.filmo-row > b > a')]

    # 随机选择五个电影并输出
    movies_text.delete('1.0', 'end')
    if len(movie_titles) == 0:
        movies_text.insert('end', '该演员暂无电影作品。')
    else:
        movies_text.insert('end', '演员 %s 的随机电影清单:\n' % (actor_name))
        for i in range(5):
            movie_title = random.choice(movie_titles)
            movies_text.insert('end', movie_title + '\n')

# 运行GUI
root.mainloop()

这个程序通过Tkinter创建了一个简单的GUI界面,包含一个文本框和一个按钮。用户在文本框中输入演员姓名后点击按钮,程序将执行电影爬取操作,随机选择五个电影并在文本框中输出结果。如果输入的演员姓名无法找到对应信息,或者演员没有电影作品,程序将给出相应的提示。

此外,如果想要随机爬取其他类型的电影信息,只需要将程序中的网址和选择器相应地修改。例如,更改url变量中的’s’参数可以搜索电视剧或其他类型的信息。