想要了解最近電影院播放的都有什麼影視電影,哪部值得推薦,如果單純靠手動收搜不全面,下面我們就先使用urllib.request模塊抓取整個網頁,再使用re模塊獲取電影信息,做個簡單的爬蟲做個數據分析。
編寫simpleCrawlerNowMoive.py代碼如下
#!/usr/bin/env python # coding: utf-8 __author__ = 'www.py3study.com' import re import urllib.request class TodayMoive(object): def __init__(self): self.url = 'https://movie.douban.com/people/1166776/' self.timeout = 3 self.filename = 'todaymoive.txt' '''內部變量定義完畢''' self.getmoiveinfo() def getmoiveinfo(self): response = urllib.request.urlopen(self.url, timeout=self.timeout) content = response.read().decode('utf-8') #findall匹配電影名字的段落 moivelist = re.findall('class="cover"><img alt="', '') st = st.replace('"', '') #split字符串切割,以' '空格爲分隔符,取第0個值 st = st.split(' ')[0] return st if __name__ == '__main__': tm = TodayMoive()
應該看到的結果
會在當前目錄下生成一個todaymoive.txt文件,內容如下。
如果單從這些代碼看起來是不是以爲不是爬蟲,其實嚴格意義上來說這個就是爬蟲,只是相對來說內容簡單,爬取的數據相對較少。大體上的爬蟲框架是這麼回事,如果用來爬取大型項目也是沒有問題的。