艺恩网内地总票房排名Top100信息及其豆瓣评分详情爬取


前两天用python2写的一个小爬虫

主要实现了从http://www.cbooo.cn/Alltimedomestic这么个网页中爬取每一部电影的票房信息等,以及在豆瓣上该电影的评分信息

代码如下

# -*- coding:utf-8 -*-
from __future__ import print_function
import urllib2
import re
'''
TODO:error 10060
'''
def fixEnglishName(name):
    ooo=re.compile("ö")
    space=re.compile("·")
    if(len(space.findall(name))!=0):
        nameTmp=re.sub("·"," ",name)
        if(len(ooo.findall(nameTmp))!=0):
            nameTtmp=re.sub("ö","o",nameTmp)
            return nameTtmp
        return nameTmp
    if(len(ooo.findall(name))!=0):
        nameTttmp=re.sub("ö","o",name)
        return nameTttmp
    return name
print(u'影片名;影片类型;国家及地区;总票房;平均票价;上映日期;场均人次;导演;主演;制作公司;发行公司;豆瓣评分    评论数;5星百分比;4星百分比;3星百分比;2星百分比;1星百分比')
pre_url="http://www.cbooo.cn/BoxOffice/getInland?pIndex="
for index in range(5):
    aft_url=str(index+1)+"&t=0"
    url=pre_url+aft_url
    response = urllib2.urlopen(url)
    pageCode=response.read().decode('utf-8')
    pattern=re.compile(".*?ID\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?rice\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\"",re.S)
    items=re.findall(pattern, pageCode)
    #pageFilms = []
    '''item[0]:id,item[1]:名字,item[2]:类型,item[3]:国家及地区,item[4]:总票房,item[5]:平均票价,item[6]:上映日期,item[7]:场均人次'''
    for item in items:
        print(item[1]+";"+item[2]+";"+item[3]+";"+item[4]+";"+item[5]+";"+item[6]+";"+item[7],end=";")
        #pageFilms.append([item[0].strip(),item[1].strip(),item[2].strip(),item[3].strip(),item[4].strip(),item[5].strip(),item[6].strip(),item[7].strip()])
        filmUrl='http://www.cbooo.cn/m/'+str(item[0])
        '''filmUrl:艺恩网电影页面'''
        filmResponse=urllib2.urlopen(filmUrl)
        filmPageCode=filmResponse.read().decode('utf-8')
        #filmPattern=re.compile("onerror=\".*?\"borbg.pad02\".*?title=\"(.*?)\">.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">",re.S)
        filmPattern = re.compile(
            "onerror=\".*?\"borbg.pad02\".*?title=\"(.*?)\">.*?<dd>.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">",
            re.S)
        filmItems=re.findall(filmPattern,filmPageCode)
        replaceSpace = re.compile("&#183;")
        for filmItem in filmItems:
            print(fixEnglishName(filmItem[0]),end=';')
            print(fixEnglishName(filmItem[1]),end=';')
            print(filmItem[2],end=';')
            print(filmItem[3],end=';')
        dbTotal_Url="https://movie.douban.com/j/subject_suggest?q="+item[1]#电影名搜索链接
        dbResponse = urllib2.urlopen(dbTotal_Url.encode("utf-8"))
        dbCode = dbResponse.read().decode('utf-8')
        dbTmp = re.sub(re.compile("\\\/"), "/", dbCode)
        dbPattern = re.compile("url\":\"(.*?)\",\"", re.S)
        dbItems = re.findall(dbPattern, dbTmp)
        for dbItem in dbItems:
            '''访问该页面并提取评分,评论数'''
            dbFilmResponse=urllib2.urlopen(dbItem.strip())
            dbFilmPageCode=dbFilmResponse.read().decode('utf-8')
            dbFilmPattern=re.compile("property=\"v:average\">(.*?)<.*?votes\">(.*?)<.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%",re.S)
            dbFilmItems=re.findall(dbFilmPattern,dbFilmPageCode)
            '''dbFilmItem[0]:评分,dbFilmItem[1]:评论数,dbFilmItem[2]:5星百分比,dbFilmItem[3]:4星百分比,dbFilmItem[4]:3星百分比,dbFilmItem[5]:2星百分比,dbFilmItem[6]:1星百分比'''
            for dbFilmItem in dbFilmItems:
                for x in range(7):
                    print(dbFilmItem[x],end=';')
            break
        print ('')
        '''换行'''

爬取过程还算顺利,期间遇到了一些小麻烦:

一部分导演的名字带有空格,由于编码的问题输出结果会变成&#183;

《一条狗的使命》的导演莱塞·霍尔斯道姆先生的英文名中某个奇怪字符(貌似是瑞典字符?)会输出成为&amp;amp;#246;

以上都通过fixEnglishName函数进行了转化.

由于输出后的结果想要直接拿到excel里使用,为了进行输出格式的控制,通过from __future__ import print_function

将print xxx 替换为 print(xxx,end='xx'),其中第二个参数省略则默认是换行

豆瓣信息的获取是从艺恩网捕获到电影名后放入豆瓣电影搜索,再进入详情页获得

关于最上面的'''todo''',找我做这个小爬虫的同学在运行我程序的时候经常会出现error10060,本来想通过多次请求连接解决来着,但是写完后我这里已经有了完整的数据了,就不需要再对本程序进行完善了

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM