【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
待续
赠人玫瑰
手留余香
我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容……我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系!-杨绛先生
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【关注我】。

浙公网安备 33010602011771号