某网站电影详情爬取

网站首页:网站首页链接
我们以爬取最新电影为例:最新电影链接

先获取电影的URL,然后在根据URL来分析网页,最后提取我们需要的信息。我们检查网页源码可以发现,我们所要提取的每一部电影的详细URL在div class="co_content8"下面的每一个a下面的href属性,只要找到每一部电影的URL,我们离爬取电影详情就更加靠近了。

获取每一部电影的详细URL

import requests
from lxml import etree
header = {"User-Agent" :
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
          "Referer" : "https://www.dytt8.net/"}


BASE_DOMAIN = 'https://www.dytt8.net/html/gndy/dyzz/list_23_1.html'
response = requests.get(BASE_DOMAIN, headers=header)

text = response.text
html = etree.HTML(text)

all_a = html.xpath("//div[@class='co_content8']//a")
for a in all_a:
    b = "https://www.dytt8.net"
    href = a.xpath("@href")[0]
    if href.startswith('/'):
        a_href = b + href
        # print(a_href)
        url = a_href
        print(url)

接下来就是最重要的了,对我们获取到的电影详细页面进行解析,提取我们需要的信息。

首先获取电影的发布时间和海报的链接:

response = requests.get(url, headers=header)
        text = response.content.decode('gbk')
        html = etree.HTML(text)

        movie = {}
        time = html.xpath("//div[@class='co_content8']/ul//text()")[0].strip()  # strip用来清除空格
        movie['time'] = time
        image = html.xpath("//div[@id='Zoom']//img/@src")[0]
        movie['image'] = image

接下来就是获取电影的名字,产地,导演等信息:

Zooms = html.xpath("//text()")
        for index, info in enumerate(Zooms):
            if info.startswith("◎年  代"):
                info = info.replace("◎年  代", "").strip()
                movie['info'] = info

            elif info.startswith("◎主  演"):
                actors = []
                for x in range(index + 1, len(Zooms)):
                    actor = Zooms[x].strip()
                    # print(actor)
                    if actor.startswith("◎"):
                        break
                    actors.append(actor)
                movie['actor'] = actors
                print(movie)

最后就是我们的运行结果了(示例):
在这里插入图片描述
之后我们可以按照需求,将我们最终爬取到的详细信息保存到Mongodb或者是Mysql当中,这里就不在演示怎么保存了!好了,到这里我们这个爬虫小项目就算是完成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值