首页 > 代码库 > python爬虫——抓取电影天堂电影信息
python爬虫——抓取电影天堂电影信息
做个小练习,抓取的是电影天堂里面最新电影的页面。链接地址:http://www.dytt8.net/html/gndy/dyzz/index.html
首先我们需要获取里面电影详情的网页地址:
import urllib2 import os import re import string # 电影URL集合 movieUrls = [] # 获取电影列表 def queryMovieList(): url = ‘http://www.dytt8.net/html/gndy/dyzz/index.html‘ conent = urllib2.urlopen(url) conent = conent.read() conent = conent.decode(‘gb2312‘,‘ignore‘).encode(‘utf-8‘,‘ignore‘) pattern = re.compile (‘<div class="title_all"><h1><font color=#008800>.*?</a>></font></h1></div>‘+ ‘(.*?)<td height="25" align="center" bgcolor="#F4FAE2"> ‘,re.S) items = re.findall(pattern,conent) str = ‘‘.join(items) pattern = re.compile (‘<a href="http://www.mamicode.com/(.*?)" class="ulink">(.*?)</a>.*?<td colspan.*?>(.*?)</td>‘,re.S) news = re.findall(pattern, str) for j in news: movieUrls.append(‘http://www.dytt8.net‘+j[0])
抓取详情页中的电影数据
def queryMovieInfo(movieUrls): for index, item in enumerate(movieUrls): print(‘电影URL: ‘ + item) conent = urllib2.urlopen(item) conent = conent.read() conent = conent.decode(‘gb2312‘,‘ignore‘).encode(‘utf-8‘,‘ignore‘) movieName = re.findall(r‘<div class="title_all"><h1><font color=#07519a>(.*?)</font></h1></div>‘, conent, re.S) if (len(movieName) > 0): movieName = movieName[0] + "" # 截取名称 movieName = movieName[movieName.find("《") + 3:movieName.find("》")] else: movieName = "" print("电影名称: " + movieName.strip()) movieContent = re.findall(r‘<div class="co_content8">(.*?)</tbody>‘,conent , re.S) pattern = re.compile(‘<ul>(.*?)<tr>‘, re.S) movieDate = re.findall(pattern,movieContent[0]) if (len(movieDate) > 0): movieDate = movieDate[0].strip() + ‘‘ else: movieDate = "" print("电影发布时间: " + movieDate[-10:]) pattern = re.compile(‘<br /><br />(.*?)<br /><br /><img‘) movieInfo = re.findall(pattern, movieContent[0]) if (len(movieInfo) > 0): movieInfo = movieInfo[0]+‘‘ # 删除<br />标签 movieInfo = movieInfo.replace("<br />","") # 根据 ◎ 符号拆分 movieInfo = movieInfo.split(‘◎‘) else: movieInfo = "" print("电影基础信息: ") for item in movieInfo: print(item) # 电影海报 pattern = re.compile(‘<img.*? src="http://www.mamicode.com/(.*?)".*? />‘, re.S) movieImg = re.findall(pattern,movieContent[0]) if (len(movieImg) > 0): movieImg = movieImg[0] else: movieImg = "" print("电影海报: " + movieImg) pattern = re.compile(‘<td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="http://www.mamicode.com/(.*?)">.*?</a></td>‘, re.S) movieDownUrl = re.findall(pattern,movieContent[0]) if (len(movieDownUrl) > 0): movieDownUrl = movieDownUrl[0] else: movieDownUrl = "" print("电影下载地址:" + movieDownUrl + "") print("------------------------------------------------\n\n\n")
执行抓取
if __name__==‘__main__‘: print("开始抓取电影数据"); queryMovieList() print(len(movieUrls)) queryMovieInfo(movieUrls) print("结束抓取电影数据")
python爬虫——抓取电影天堂电影信息
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。