用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()