【Python】计算豆瓣电影TOP250的平均得分

用python写的爬虫练习,感觉比golang要好写一点。

import re
import urllib

origin_url = 'https://movie.douban.com/top250?start=00&filter='
urls = []
scores = []


def get_url():
    step = 0
    while step <= 250:
        tmp = origin_url[:38]
        tmp += str(step)
        tmp += origin_url[40:]
        urls.append(tmp)
        step += 25

def get_html(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html


def get_score(html):
    score = []
    reg = r'property="v:average">([0-9].[0-9])</span>'
    score = re.findall(re.compile(reg), html)
    return score


def solve():
    get_url()
    for each in urls:
        print each
        scores.append(get_score(get_html(each)))
    sum = 0
    cnt = 0
    for each in scores:
        if cnt == 250: break
        for i in range(0, len(each)):
            if cnt == 250: break
            cnt += 1
            sum += float(each[i])
    return sum / 250

print solve()

 

 

参考:https://www.cnblogs.com/kirai/p/5756854.html

待续

posted @ 2019-03-22 15:54  念槐聚  阅读(273)  评论(0)    收藏  举报