请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python命令行IMDB爬虫(Scraper)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

概览

这个脚本要求输入一个电影标题和年代,然后查询IMDB获得电影信息。

命令行IMDB Scraper

第一步是导入必要的模块。

#!/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 3.3:


#!/usr/bin/env python27

#Importing the modules

from bs4 import BeautifulSoup

import sys

import urllib.request

import urllib.error

import re

import json

#Ask for movie title

title = input("Please enter a movie title: ")

#Ask for which year

year = 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 = urllib.request.Request(url)

response = json.loads(urllib.request.urlopen(request).read().decode("utf-8"))

print(json.dumps(response,indent=2))

参考资料

  • Python Command Line IMDB Scraper

鲜花

握手

雷人

路过

鸡蛋
专题导读
上一篇:
JavaScript调试利器console.log()发布时间:2022-05-14
下一篇:
Scala编程常见问题整理【十六】发布时间:2022-05-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap