python实现根据用户输入从电影网站获取影片信息的方法

yipeiwu_com6年前Python基础

本文实例讲述了python实现根据用户输入从电影网站获取影片信息的方法。分享给大家供大家参考。具体如下:

这段python代码主要演示了用户终端输入,正则表达式,网页抓取等

#!/usr/bin/env python27
#Importing the modules
from BeautifulSoup import BeautifulSoup
import sys
import urllib2
import re
import json
#Ask for movie title
title = raw_input("Please enter a movie title: ")
#Ask for which year
year = raw_input("which year? ")
#Search for spaces in the title string
raw_string = re.compile(r' ')
#Replace spaces with a plus sign
searchstring = raw_string.sub('+', title)
#Prints the search string
print searchstring
#The actual query
url = "http://www.imdbapi.com/?t=" + searchstring + "&y="+year
request = urllib2.Request(url)
response = json.load(urllib2.urlopen(request))
print json.dumps(response,indent=2)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python3 把一个列表按指定数目分成多个列表的方式

如果我们需要将一个列表按指定数目分成多个列表:比如[1,2,3,4,5,6,7,8,9,10]分成[1,2,3][4,5,6][7,8,9][10],我们可以建立一个列表分割的函数spl...

使用PyInstaller将Python程序文件转换为可执行程序文件

Windows下采用PyInstall将py文件转换成exe可执行文件 好不容易写完的py文件,想做成exe文件,最开始选择用py2exe,结果生成的exe遇到两个问题, 1. py程序...

浅析python递归函数和河内塔问题

关于递归函数:   函数内部调用自身的函数。 以n阶乘为例:   f(n) = n ! = 1 x 2 x 3 x 4 x...x(n-1)x(n) = n x (n-1) ! de...

Python如何调用JS文件中的函数

Python如何调用JS文件中的函数

Python 调用JS文件中的函数方法如下 1、安装PyExecJS第三方库 2、导入库:import execjs 3、调用JS文件中的方法 Passwd = execjs.c...

Python 3.8 新功能全解

Python 3.8是Python语言的最新版本,它适合用于编写脚本、自动化以及机器学习和Web开发等各种任务。现在Python 3.8已经进入官方的beta阶段,这个版本带来了许多语法...