1. 程式人生 > >基於python2的豆瓣Top250爬蟲練習

基於python2的豆瓣Top250爬蟲練習

range turn 一個 open num dal python2 .com python

 1 # coding=utf-8
 2 import urllib
 3 import re
 4 #獲取源碼
 5 def gethtml(pg):
 6     url = https://movie.douban.com/top250?start=%d&filter= % pg
 7     html = urllib.urlopen(url).read()
 8     return html
 9 
10 #爬取數據
11 if __name__ == __main__:
12     pat = re.compile(<em class="">(.*?)</em>.*?<a href="(.*?)">.*?<img.*?alt="(.*?)" src="(.*?)".*?>
,re.S) 13 for i in range(0,226,25): 14 html = gethtml(i) 15 listnum = re.findall(pat,html) #findall返回的是一個tuple 16 for i in range(25): 17 for j in range(4): 18 print listnum[i][j]

基於python2的豆瓣Top250爬蟲練習