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

相关文章

利用python修改json文件的value方法

做工程时遇到需要监听json文件,根据json文件中的key-value值作出相应处理的情形。为此写了修改json文件的python脚本供工程后续调用。 代码如下: # coding...

python 专题九 Mysql数据库编程基础知识

python 专题九 Mysql数据库编程基础知识

在Python网络爬虫中,通常是通过TXT纯文本方式存储,其实也是可以存储在数据库中的;同时在WAMP(Windows、Apache、MySQL、PHP或Python)开发网站中,也可以...

python面向对象之类属性和类方法案例分析

python面向对象之类属性和类方法案例分析

本文实例讲述了python面向对象之类属性和类方法。分享给大家供大家参考,具体如下: 目标 类的结构 类属性和实例属性 类方法和静态方法 01. 类的结构 1.1 术语 —— 实例 使用...

使用python读取txt文件的内容,并删除重复的行数方法

注意,本文代码是使用在txt文档上,同时txt文档中的内容每一行代表的是图片的名字。 #coding:utf-8 import shutil readDir = "原文件绝对路经...

详解django2中关于时间处理策略

详解django2中关于时间处理策略

一、django中数据模型关于时间字段的认识 1、 DateField :可以记录年月日,映射到数据库是 date 类型 2、 DateTimeField :可以记录年月日时分秒,映射到...