IMDB-TOP_250-爬虫
这个小学期Python大作业搞了个获取IMDB TOP 250电影全部信息的爬虫。第二次写爬虫,比在暑假集训时写的熟练多了。欢迎大家评论。
'''
************************************************
*Time:2017.9.11
*Target:All movies' information of IMDB TOP_250
*Resources:http://www.imdb.cn/IMDB250/
************************************************
''' import re
import requests
import numpy as np
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup num = 1 #电影计数
All_txt = [] #全部电影的信息
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}#浏览器代理
def getHTMLText(url):
try:
#print(url)
r = requests.get( url,headers = headers )
#print(r)
r.encoding = 'utf-8'
return r.text
except:
return "错误" #从每一部电影的页面中获取全部信息
def get_all_information(url,page):
global num,All_txt
txt = getHTMLText(url)
if txt != "错误":
print('page'+str(page)+' NO.'+str(num)+' Get it!')
if num == 247:
print('Finished!!!')
soup = BeautifulSoup(txt,"html.parser")
Cname,Ename,Score,title,Actor,Starring,Infor = '','','','','','','' #TOP250-film_Chinese_name&Score
infor_1 = soup.find_all('div',class_ = 'hdd')
rel = '<h3>'+'[\s\S]*?'+'</h3>'
pattern = re.compile(rel)
Cname = ''.join(pattern.findall(str(infor_1[0])))
Cname = Cname.replace('<h3>','').replace('</h3>','')
#print(Cname)
#find_the_year & save
rel = '('+'[\s\S]*?'+')'
pattern = re.compile(rel)
time_ = ''.join(pattern.findall(Cname))
#print(time_)
with open('time.txt','a',encoding='utf-8') as t:
t.write( time_.replace('(','').replace(')','') + '\n' )
#find_Score
rel = '<i>'+'[\s\S]*?'+'</i>'
pattern = re.compile(rel)
Score = ''.join(pattern.findall(str(infor_1[0])))
Score = Score.replace('<i>','').replace('</i>','')
#print(Cname,Score) #TOP250-film_many_infor
now = soup.find_all('div',class_ = 'bdd clear')
#print(now[0])
a = BeautifulSoup(str(now[0]), "html.parser")
many_infor = a.find_all('li') #TOP250-film_Ename
Ename = str(many_infor[0]).replace('<li>','').replace('<i>','').replace('</i>','').replace('</li>','').replace('<a>','').replace('</a>','')
#TOP250-film_Actor
Actor_temp = BeautifulSoup(str(many_infor[2]), "html.parser").find_all('a')
Actor = Actor_temp[0].get_text().replace('导演:','')
#TOP250-film_Starring
Starring_temp = BeautifulSoup(str(many_infor[3]), "html.parser").find_all('a')
for i in Starring_temp:
Starring += i.get_text().replace(' ','') + ' '
#print(Starring) #Top-film_Infor
for j in range(4,7):
Infor_temp = BeautifulSoup(str(many_infor[j]), "html.parser")
for i in Infor_temp.children:
Infor += i.get_text().replace(' ','') + ' '
Infor += '\n'
#print(Infor) #TOP250-film_Synopsis
content = soup.find_all('div',class_ = 'fk-4 clear')
#print(content)
soup_con = BeautifulSoup(str(content[0]), "html.parser")
title = soup_con.find_all('div',class_ = 'hdd')
title = str(title[0]).replace('<div class="hdd">','').replace('</div>','\n')
#print(title)
content_1 = soup_con.find_all('div',class_ = 'bdd clear')
content_1 = str(content_1[0]).replace('<div class="bdd clear" style="font-size:15px">','').replace('</div>','')
content_1 = content_1.replace('<!-- <p><a href="#">更多剧情 >></a></p> -->','').replace('<br/>','\n') #Save_all_information
All_txt.append('第'+str(num)+'部'+'\n')
All_txt.append( Cname+'\n' )
All_txt.append( '【英文名】'+Ename+'\n' )
All_txt.append( '【评分】'+Score+'\n' )
All_txt.append( '【导演】'+Actor+'\n' )
All_txt.append( '【主演】'+Starring+'\n' )
All_txt.append( Infor+'\n' )
All_txt.append( title+'\n'+content_1+'\n' )
All_txt.append('\n')
num += 1 #在每一页中得到当前页的全部电影的url
def getin_one(url,page):
txt = getHTMLText(url)
soup = BeautifulSoup(txt, "html.parser")
#print(soup)
temp = soup.find_all('div',class_="ss-3 clear")
rel = '<a href="' + '[\s\S]*?' + '">'
pattern = re.compile(rel)
All_url = pattern.findall( str(temp[0]) )
for i in range(len(All_url)):
temp_url = 'http://www.imdb.cn'+All_url[i].replace('<a href="','').replace('">','')
get_all_information(temp_url,page)
#print(All_url) #将所有电影的年份统计并生成条形图
def Analyze_some_infor():
plt.rc('font', family='SimHei', size=13)#字体及大小
#Analyze_time
file = open('time.txt')
a,b,c,d,e,f = 0,0,0,0,0,0
for line in file:
line = eval(line)
if line == 0:
f += 1
elif line < 1940 and line >= 1920:
a += 1
elif line < 1960 and line >= 1940:
b += 1
elif line < 1980 and line >= 1960:
c += 1
elif line < 2000 and line >= 1980:
d += 1
else:
e += 1
times = [a,b,c,d,e,f]
range_time = ['1920-1940','1940-1960','1960-1980','1980-2000','2000-现在','无信息']
idx = np.arange(len(range_time))
width = 0.5
plt.bar(idx,times,width,color='green')
plt.xticks(idx+width/2, range_time, rotation=40)
plt.xlabel('电影年代')
plt.ylabel('数目')
plt.savefig('time_pic.jpg')
plt.show() def main():
global All_txt
getin_one('http://www.imdb.cn/IMDB250/',1)
for i in range(2,10):
getin_one( 'http://www.imdb.cn/imdb250/'+str(i) , i )
#将已有内容清空
with open('All_infor.txt','w',encoding='utf-8') as x:
pass
with open('All_infor.txt','a',encoding='utf-8') as x:
for i in All_txt:
x.write(i)
Analyze_some_infor() main()
作者: LB919
出处:http://www.cnblogs.com/L1B0/
该文章为LB919投入了时间和精力的原创;
如有转载,荣幸之至!请随手标明出处;
IMDB-TOP_250-爬虫的更多相关文章
- IMDB TOP 250爬虫
这个小学期Python大作业搞了个获取IMDB TOP 250电影全部信息的爬虫.第二次写爬虫,比在暑假集训时写的熟练多了.欢迎大家评论. ''' ************************** ...
- python爬虫https://www.imdb.com/chart/top的电影
目标:爬取https://www.imdb.com/chart/top网页上面的电影top20 直接上main.py代码: #!/usr/bin/python35 # -*- coding:utf-8 ...
- Java豆瓣电影爬虫——抓取电影详情和电影短评数据
一直想做个这样的爬虫:定制自己的种子,爬取想要的数据,做点力所能及的小分析.正好,这段时间宝宝出生,一边陪宝宝和宝妈,一边把自己做的这个豆瓣电影爬虫的数据采集部分跑起来.现在做一个概要的介绍和演示. ...
- 利用Abot爬虫和visjs 呈现漫威宇宙
1. 引言 最近接触Abot爬虫也有几天时间了,闲来无事打算从IMDB网站上爬取一些电影数据玩玩.正好美国队长3正在热映,打算爬取漫威近几年的电影并用vis这个JS库呈现下漫威宇宙的相关电影. Abo ...
- Abot爬虫和visjs
1. 引言 最近接触Abot爬虫也有几天时间了,闲来无事打算从IMDB网站上爬取一些电影数据玩玩.正好美国队长3正在热映,打算爬取漫威近几年的电影并用vis这个JS库呈现下漫威宇宙的相关电影. Abo ...
- Python爬虫 -- 抓取电影天堂8分以上电影
看了几天的python语法,还是应该写个东西练练手.刚好假期里面看电影,找不到很好的影片,于是有个想法,何不搞个爬虫把电影天堂里面8分以上的电影爬出来.做完花了两三个小时,撸了这么一个程序.反正蛮简单 ...
- python增量爬虫pyspider
1.为了能够将爬取到的数据存入本地数据库,现在本地创建一个MySQL数据库example,然后 在数据库中建立一张表格test,示例如下: DROP TABLE IF EXISTS `test`; C ...
- 一个简单的python爬虫程序
python|网络爬虫 概述 这是一个简单的python爬虫程序,仅用作技术学习与交流,主要是通过一个简单的实际案例来对网络爬虫有个基础的认识. 什么是网络爬虫 简单的讲,网络爬虫就是模拟人访问web ...
- 我们的爬虫从pyspider开始说起(一)
看各种爬虫文献也有好几天了,总是感觉下不了手,总结一句“提笔忘字,总是因为看的太多而写的太少”.所以从现在开始,把看到的想到的,需要总结的东西慢慢的都沉淀下来,扎扎实实的走好每一步. 先来说这几天遇到 ...
随机推荐
- Microsonf visual c++ 14+ 离线内网安装
内网离线安装方法:先下载官方的visualcppbuildtools: <br href=http://go.microsoft.com/fwlink/?LinkId=691126 >& ...
- RFC3984: RTP Payload Format for H.264 Video(中文版)
转载地址:https://blog.csdn.net/h514434485/article/details/51010950 官方文档,中文版本地址:http://www.rosoo.net/File ...
- STL-C - 稳定排序
C - 稳定排序 大家都知道,快速排序是不稳定的排序方法.如果对于数组中出现的任意a[i],a[j](i<j),其中a[i]==a[j],在进行排序以后a[i]一定出现在a[j]之前,则认为该排 ...
- 结合字符串常量池/String.intern()/String Table来谈一下你对java中String的理解
1.字符串常量池 每创建一个字符串常量,JVM会首先检查字符串常量池,如果字符串已经在常量池中存在,那么就返回常量池中的实例引用.如果字符串不在池中,就会实例化一个字符串放到字符串池中.常量池提高了J ...
- 第四十七篇 入门机器学习——分类的准确性(Accuracy)
No.1. 通常情况下,直接将训练得到的模型应用于真实环境中,可能会存在很多问题 No.2. 比较好的解决方法是,将原始数据中的大部分用于训练数据,而留出少部分数据用于测试,即,将数据集切分成训练数据 ...
- APP 安全测试
http://www.cnblogs.com/wetest/p/6694529.html
- testng的prioriy
todo: 同一个class中的priority: 1.不标priority的case和标注priority的case,谁先谁后? 2.标注相同priority的case,谁先谁后?是不是并发? 3. ...
- TFTP服务[精简版]:简单文件传输协议
简单文件传输协议(Trivial File Transfer Protocol,TFTP)是一种基于 UDP 协议在客户端 和服务器之间进行简单文件传输的协议.顾名思义,它提供不复杂.开销不大的文件传 ...
- 百炼OJ - 1001 - Exponentiation
题目链接 哇一遍AC的感觉也太爽了吧:) #include <stdio.h> #include <string.h> int times=0; char *myCalc(ch ...
- JS高级---把局部变量变成全局变量
如何把局部变量变成全局变量? 把局部变量给window就可以了 函数的自调用---自调用函数 一次性的函数--声明的同时, 直接调用了 (function () { console.log(& ...