电影天堂数据采集需求文档(先用正则表达式做)
一、 最新电影页面电影链接采集
1. 首页url:http://www.ygdy8.net/html/gndy/dyzz/list_23_1.html
2. 从该页面根据正则匹配数据,具体要求如下:
2.1 匹配电影名称 例如:2017年动作冒险《太空救援》BD俄语中字
2.2 匹配电影详情页链接地址 例如:/html/gndy/dyzz/20180107/55981.html
3. 分析url地址匹配前10页数据
二、 根据电影详情页的url地址,采集电影详情信息
1. 根据正则,匹配数据,具体如下:
1.1 匹配电影名称 例如:太空救援/礼炮号空间站
1.2 匹配电影类别 例如: 动作/冒险
1.3 匹配电影上映日期 例如:2017-10-05(俄罗斯)/2018-01-12(中国)
1.4 匹配电影片长 例如:111分钟
1.5 匹配电影下载地址 例如:ftp://ygdy8:ygdy8@yg45.dydytt.net:3026/[阳光电影www.ygdy8.net].太空救援.BD.720p.俄语中字.mkv
1.6 如需其他数据,自行书写代码
2. 将所有匹配到的数据写入excel表格中进行存储,存储格式如下图:
注:以类和对象的方式去写这个爬虫,要求写两个爬虫类,一个用于爬取列表页的电影详情页地址,一个用于爬取电影详细信息数据,并保存到excel文件中。
xpath基本语法
# coding: utf-8
import requests
# lxml 包 专门用处理一些html和xml的 打开cmd命令 输入pip install lxml
# 引入etree模块,,从lxml包中引入我们需要的etree
from lxml import etree
# xpath 一组特定的规则,可以根据特殊的语法结构, =headers)
# 将html源代码转换为树形结构数据
html = etree.HTML(resposne.content)
# 根据xpath从html中提取img标签,返回的是列表,列表中存放就是找到的标签对象
# imgs = html.xpath('//img')
# for img in imgs:
# # 查找img标签src属性值
# src = img.xpath('@src')
# print src
links = html.xpath('//a[@class="archive-title"]/text()')
for link in links:
# 找到href属性值,返回的是列表,可以取出列表中的第0个元素
href = link.xpath('@href')[0]
title = link.xpath('text()')[0]
print u'博客标题:%s 链接:%s'%(title, href)
例子
利用xpath爬取电影天堂,将数据存储在表格中
# coding: utf-8
import requests
from lxml import etree
import xlwt
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
}
# 1.url
url = 'http://www.ygdy8.net/html/gndy/dyzz/index.html'
# 2.发起请求,接受响应
response = requests.get(url, headers=headers)
# 3.转成树形节点结构
html = etree.HTML(response.content)
# //select[@name="sldd"]/option[last()]/text() last()找到多个标签中的最后一个标签
total_page = html.xpath('//select[@name="sldd"]/option[last()]/text()')[0]
print '共有%s页电影信息,正在准备爬取!'%total_page
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet(u'最新电影信息')
sheet.write(0, 0, '电影名称')
sheet.write(0, 1, '电影类型')
sheet.write(0, 2, '电影时长')
sheet.write(0, 3, '电影下载地址')
count = 0
# 循环遍历所有页
for x in range(1,int(total_page)+1):
print '正在爬取第%s页数据,请稍后....'%x
# 根据x的值,拼接完整页面url地址
url = 'http://www.ygdy8.net/html/gndy/dyzz/list_23_%s.html'%x
response = requests.get(url, headers=headers)
html = etree.HTML(response.content)
# 4.使用xpath查找所有的href属性值
hrefs = html.xpath('//a[@class="ulink"]/@href')
# for循环取出所有的href值
for href in hrefs:
count += 1
print '正在爬取第%s个电影信息..'%count
# 拼接完整的url地址
detail_url = 'http://www.ygdy8.net%s'%href
# 发送请求,拿回详情页面的数据
detail_response = requests.get(detail_url, headers=headers)
# print detail_response.content
# 转换树形节点结构
detail_html = etree.HTML(detail_response.content)
# 根据xpath从详情页提取数据
movie_info = detail_html.xpath('//div[@id="Zoom"]//text()')
# for中存放的就是电影的所有信息
for movie in movie_info:
if u'译 名' in movie:
movie_name = movie.split(u' ')[-1]
elif u'类 别' in movie:
movie_type = movie.split(u' ')[-1]
elif u'片 长' in movie:
movie_time = movie.split(u' ')[-1]
download_url = detail_html.xpath('//tbody/tr/td/a/@href')[0]
sheet.write(count, 0,movie_name)
sheet.write(count, 1,movie_type)
sheet.write(count, 2,movie_time)
sheet.write(count, 3,download_url)
workbook.save(u'电影天堂数据.xls')