前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python etree解析豆瓣电影TOP250

Python etree解析豆瓣电影TOP250

作者头像
小锋学长生活大爆炸
发布2021-03-23 10:58:15
3640
发布2021-03-23 10:58:15
举报
文章被收录于专栏:小锋学长生活大爆炸
代码语言:javascript
复制
#coding: utf-8
import requests
from lxml import etree
import xlwt
import os

class Solution:
    def __init__(self):
        pass

    def getItems(self, url):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45"
        }
        html = requests.get(url, headers=headers).text
        element = etree.HTML(html)
        tree = element.xpath('//*[@id="content"]/div/div[1]/ol/li')
        return tree

    def parseItem(self, tree):
        results = []
        for item in tree:
            resMap = {}
            node = etree.HTML(etree.tostring(item[0], encoding="utf-8").decode())
            title = ''.join(node.xpath('//div/div[2]/div[1]/a/span/text()'))
            actor = node.xpath('//div/div[2]/div[2]/p[1]/text()')[0].replace(" ", "").strip('\n')
            year = node.xpath('//div/div[2]/div[2]/p[1]/text()')[1].replace(" ", "").strip('\n')
            desc = node.xpath('//div/div[2]/div[2]/p[2]/span/text()')[0]
            score = node.xpath('//div/div[2]/div[2]/div/span[2]/text()')[0]
            comment = node.xpath('//div/div[2]/div[2]/div/span[4]/text()')[0]
            mvlink = node.xpath('//div/div[2]/div[1]/a/@href')[0]
            piclink = node.xpath('//div/div[1]/a/img/@src')[0]
            resMap['title'] = title
            resMap['score'] = score
            resMap['comment'] = comment
            resMap['actor'] = actor
            resMap['year'] = year
            resMap['desc'] = desc
            resMap['mvlink'] = mvlink
            resMap['piclink'] = piclink
            results.append(resMap.copy())
        return results

    def saveXlwt(self, datalist, savepath):
        book = xlwt.Workbook(encoding="utf-8", style_compression=0)
        sheet = book.add_sheet('豆瓣电影top250', cell_overwrite_ok=True)
        col = ('影片名', '演员', '年份', '评分', '评价数', '概况', '电影链接详情', '图片链接')
        for i in range(0, len(col)):
            sheet.write(0, i, col[i])
        for i in range(0, len(datalist)):
            print("第%d条" % (i+1))
            data = datalist[i]
            sheet.write(i + 1, 0, data["title"])
            sheet.write(i + 1, 1, data["actor"])
            sheet.write(i + 1, 2, data["year"])
            sheet.write(i + 1, 3, data["score"])
            sheet.write(i + 1, 4, data["comment"])
            sheet.write(i + 1, 5, data["desc"])
            sheet.write(i + 1, 6, data["mvlink"])
            sheet.write(i + 1, 7, data["piclink"])
        book.save(savepath)


if __name__ == '__main__':
    baseurl = 'https://movie.douban.com/top250?start='
    sl = Solution()
    tree = sl.getItems(baseurl)
    results = sl.parseItem(tree)
    sl.saveXlwt(results, os.path.join(os.getcwd(), "movie.xls"))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档