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

yipeiwu_com5年前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 Pycharm上连接数据库时报错的问题

最近在学习python。 今天在学习python连接Mysql数据库时报错: AttributeError: 'NoneType' object has no attribute '...

解决python web项目意外关闭,但占用端口的问题

问题描述 因为项目强制关闭,但是服务还在运行,导致重新运行项目时候 提示地址已经使用(端口被占用) /usr/bin/python3.5 python-login-demo/inde...

Python网络编程基于多线程实现多用户全双工聊天功能示例

本文实例讲述了Python网络编程基于多线程实现多用户全双工聊天功能。分享给大家供大家参考,具体如下: 在前面一篇《Python网络编程使用select实现socket全双工异步通信功能...

Python实现的读写json文件功能示例

本文实例讲述了Python实现的读写json文件功能。分享给大家供大家参考,具体如下: 相比java,python对json文件的处理就简单很多。java操作json文件的话需要引用ja...

对numpy的array和python中自带的list之间相互转化详解

a=([3.234,34,3.777,6.33]) a为python的list类型 将a转化为numpy的array: np.array(a) array([ 3.234,...