想知道一部电影好不好看,豆瓣的评分还是比较靠谱的,于是,搞了搞,写了一个小爬虫:
说明文档:
1.直接讲代码保存成.py文件
2.输入你喜欢的电影,按下回车
3.显示豆瓣的电影评分
4. 按任意键退出程序;
# -*- coding: utf-8 -*-
import urllib2
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
values = raw_input()
#data = urllib.urlencode(values)
url = 'https://www.douban.com/search?cat=1002&q='
geturl = url+values
print geturl
request = urllib2.Request(geturl)
response = urllib2.urlopen(request)
#with open('C:\\Users\\Wang Zuo\\Desktop\\test.txt', 'w') as f:
# f.write(response.read())
content = response.read().decode('utf-8') #得到网页内容
pattern = re.compile(r' (.*?)')
item = re.findall(pattern,content)
if len(item) == 0 :
print 'Did not find the data'
else:
print '评分:'+item[0]
raw_input()