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程序设计有所帮助。

相关文章

Python Mysql数据库操作 Perl操作Mysql数据库

首先下载 MySQLdb #encoding=GBK import MySQLdb #import sys # #reload(sys) #sys.setdefaultencoding(...

Python之time模块的时间戳,时间字符串格式化与转换方法(13位时间戳)

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。 关于时间戳的几个概念 时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移...

基于python二叉树的构造和打印例子

写在最前面: 带你从最简单的二叉树构造开始,深入理解二叉树的数据结构,ps:不会数据结构的程序猿只能是三流的 首先,我们构造一个二叉树 这是最标准,也是最简单的二叉树构造方法 '''...

Python3中在Anaconda环境下安装basemap包

Python3中在Anaconda环境下安装basemap包

Basemap是matplotlib子包,也是python中最常用、最方便的地理数据可视化工具之一。 在中端输入pip list先查看是否有jupyter,一般安装了Anaconda都会...

python使用sessions模拟登录淘宝的方式

之前想爬取一些淘宝的数据,后来发现需要登录,找了很多的资料,有个使用request的sessions加上cookie来登录的,cookie的获取在登录后使用开发者工具可以找到。不过这个登...