使用requests库和lxml解析爬取电影天堂电影信息

使用requests库获取电影天堂电影信息,将所有链接保存下来后可以使用迅雷批量下载。快速获得最新最全电影资源!

站点分析

以电影天堂国内电影为例
http://www.ygdy8.net/html/gndy/china/index.html

页面分析

分析其目录内每一个电影信息存在table中,首先我们要获取每一个电影的详情地址

所有电影信息的详情链接获取

通过request,获取页面源码,xpath取得所有class="tbspan"table下面的class="ulink"a标签的@href的值,由于是相对地址,因此需要与网站地址base_url进行拼接。最后得到的就是该页面所有电影的详情地址。

base_url='http://www.ygdy8.net'
url='http://www.ygdy8.net/html/gndy/china/index.html'
def get_content(url):
    resp=requests.get(url)
    text=resp.content.decode(encoding='gbk', errors='ignore')
    html=etree.HTML(text)
    urls=html.xpath('//table[@class="tbspan"]//a[@class="ulink"][2]/@href')
    detail_urls= map(lambda url:base_url+url,urls)   
    #返回信息
    return(detail_urls)

单个电影详情爬取

通过对单个电影详情页面进行分析,发现其标题位于font下,可使用title=html.xpath('//div[@class="title_all"]//font/text()')[0]获取,

标题

其介绍内容以<br>标签为间隔,保存在一个<p>标签中,而在爬取中,会将每一行的文本,作为列表中的一项,因此可以使用列表索引查找,但考虑到可能有介绍内容不同等问题,使用startswith对其开始字符进行匹配索引。例如:
@年 代 2019
开头字符为“@年 代”,我们使用定义的替换函数将开头字符替换为空字符,将其后面的内容作为年份,既可取出2019。实现代码如下:

def get_detail(url):
    movie={}
    resp=requests.get(url)
    text=resp.content.decode(encoding='gbk', errors='ignore')
    html=etree.HTML(text)
    title=html.xpath('//div[@class="title_all"]//font/text()')[0]
    movie['title']=title
    //此函数作用是对“年代”“产地”等标题字符进行空字符替换
    def parse_info(info,rule):
        return info.replace(rule,"").strip()
    infos=html.xpath('//div[@id="Zoom"]//text()')
    for index,info in enumerate(infos):
        if info.startswith('◎年\u3000\u3000代'):
            info =parse_info(info,'◎年\u3000\u3000代')
            movie['years']= info
        elif info.startswith('◎产\u3000\u3000地'):
            info =parse_info(info,'◎产\u3000\u3000地')
            movie['country']= info
        elif info.startswith('◎类\u3000\u3000别'):
            info =parse_info(info,'◎类\u3000\u3000别')
            movie['category']= info            
        elif info.startswith('◎豆瓣评分'):
            info =parse_info(info,'◎豆瓣评分')
            movie['douban']= info
        elif info.startswith('◎片\u3000\u3000长'):
            info =parse_info(info,'◎片\u3000\u3000长')
            movie['duration']= info
        elif info.startswith('◎导\u3000\u3000演'):
            info =parse_info(info,'◎导\u3000\u3000演')
            movie['directors']= info
        elif info.startswith('◎主\u3000\u3000演'):
            info =parse_info(info,'◎主\u3000\u3000演')
            actors=[info]
            for i in range(index+1,100):
                actor=infos[i].strip()
                if infos[i].startswith('◎'):
                    break;
                actors.append(actor)
            movie['actors']= actors
        elif info.startswith('◎简\u3000\u3000介'):
            info =parse_info(info,'◎简\u3000\u3000介')
            profile=infos[index+1].strip()
            movie['profile']= profile
    download_url=html.xpath('//td[@bgcolor="#fdfddf"]/a/@href')[0]
    movie['download_url']= download_url   
    return(movie)

多页面爬取

点击第二页,第三页,发现分页链接有如下结构:
http://www.ygdy8.net/html/gndy/china/list_4_{}.html,因此可使用循环,对多页面进行爬取:

def spider():
    url='http://www.ygdy8.net/html/gndy/china/list_4_{}.html'
    for i in range(1,2):
        sort_url=url.format(i)
        detail_urls=get_content(sort_url)
        for detail_url in detail_urls:
            movie=get_detail(detail_url)
            print(movie)
            
if __name__=='__main__':
    spider()

完整代码:
https://github.com/ericariel/scraper/blob/master/dytt8.py

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,491评论 1 45
  • 电影天堂里面的数据还是非常丰富的,这次的爬虫demo,是对电影天堂中的电影数据进行爬取,包括电影片名,导演,主演,...
    振礼硕晨阅读 9,012评论 0 5
  • 前言: 本文非常浅显易懂,可以说是零基础也可快速掌握。如有疑问,欢迎留言,笔者会第一时间回复。本文代码存于gith...
    Dwyane_Coding阅读 13,358评论 1 30
  • 20150913-一笑 老婆晚上煮了一锅馄饨。老公下班回到家,看见老婆揍儿子,没理他们,径直走到锅旁盛了一碗馄饨吃...
    RainyCai阅读 1,450评论 0 1
  • 《丧钟为谁而鸣》中曾写道,“没有人是一座孤岛”。在人类的传统生活中,群居的生活形态早已根深蒂固。人与人之间的联系越...
    WZ楔子阅读 4,468评论 0 2

友情链接更多精彩内容