豆瓣电影排行榜前250 分为10页,第一页的url为https://movie.douban.com/top250,但实际上应该是https://movie.douban.com/top250"_blank" href="https://movie.douban.com/top250" rel="external nofollow" >https://movie.douban.com/top250"codetitle">复制代码 代码如下:for i in range(0, 250, 25): print(i)
分析完页面组成后,开始获取页面,直接request.get()发现没有返回任何东西,输出一下响应码
url = 'https://movie.douban.com/top250"text-align: center">
import requests import lxml.etree as etree url = 'https://movie.douban.com/top250"/html/body/div[3]/div[1]/div/div[1]/ol/li[1]/div/div[2]/div[1]/a/span[1]") print(name)
但是直接这样子,解析到的是这样的结果
[<Element span at 0x20b2f0cc488>]
关于这东西是什么,有文章写的很好:https://www.jb51.net/article/132145.htm
这里我直接写解决部分,在使用xpath解析时,后面加上/text()
name = html.xpath("/html/body/div[3]/div[1]/div/div[1]/ol/li[1]/div/div[2]/div[1]/a/span[1]/text()")
解决后,再使用xpath finder插件,一步一步获取到电影所有数据
最后把这个写在函数里,外面再套上一开始说的循环,就OK了
# -*- coding: utf-8 -*- import requests import lxml.etree as etree def get_source(page): url = 'https://movie.douban.com/top250"/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[1]/a/span[1]/text()".format(i)) info = html.xpath("/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[2]/p[1]/text()".format(i)) score = html.xpath( "/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[2]/div/span[2]/text()".format(i)) slogan = html.xpath( "/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[2]/p[2]/span/text()".format(i)) print(name[0]) print(info[0].replace(' ', '')) print(info[1].replace(' ', '')) print(score[0]) print(slogan[0]) n = 1 for i in range(0, 250, 25): print('第%d页' % n) n += 1 get_source(i) print('==========================================')
在定位时,发现有4部电影介绍没有slogan,导致获取到的信息为空列表,也就导致了list.append()会出错。所以我加上了几个差错处理,解决方式可能有点傻,如果有更好的解决办法,洗耳恭听
代码在最后可以看到
EXCEL保存部分
这里我用的xlwt
book = xlwt.Workbook()
sheet = book.add_sheet(u'sheetname', cell_overwrite_ok=True)
创建一个sheet表单。
数据保存到一个大列表中,列表嵌套列表
再通过循环把数据导入到excel表单中
r = 1 for i in LIST: #有10页 for j in i: #有25条数据 c = 2 for x in j: #有5组数据 print(x) sheet.write(r, c, x) c += 1 r += 1
最后在保存一下
book.save(r'douban.xls')
注意文件后缀要用xls,用xlsx会导致文件打不开
然后就大功告成了
打开文件,手动加入排名,等部分信息(这些也可以在程序里完成,我嫌麻烦,就没写,直接手动来的快)
前面的"htmlcode">
# -*- coding: utf-8 -*- import requests import lxml.etree as etree import xlwt def get_source(page): List = [] url = 'https://movie.douban.com/top250"/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[1]/a/span[1]/text()".format(i)) info = html.xpath("/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[2]/p[1]/text()".format(i)) score = html.xpath( "/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[2]/div/span[2]/text()".format(i)) slogan = html.xpath( "/html/body/div[3]/div[1]/div/div[1]/ol/li[{}]/div/div[2]/div[2]/p[2]/span/text()".format(i)) try: list.append(name[0]) except: list.append('----') try: list.append(info[0].replace(' ', '').replace('\n', '')) except: list.append('----') try: list.append(info[1].replace(' ', '').replace('\n', '')) except: list.append('----') try: list.append(score[0]) except: list.append('----') try: list.append(slogan[0]) except: list.append('----') List.append(list) return List n = 1 LIST = [] for i in range(0, 250, 25): print('第{}页'.format(n)) n += 1 List = get_source(i) LIST.append(List) def excel_write(LIST): book = xlwt.Workbook() sheet = book.add_sheet(u'sheetname', cell_overwrite_ok=True) r = 1 for i in LIST: #有10页 for j in i: #有25条数据 c = 2 for x in j: #有5组数据 print(x) sheet.write(r, c, x) c += 1 r += 1 book.save(r'douban1.xls') #保存代码 excel_write(LIST)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】