鱼C论坛

 找回密码
 立即注册
查看: 3047|回复: 5

[技术交流] requests爬取豆瓣电影TOP250存入excel中(在不看甲鱼哥教程情况下)

[复制链接]
发表于 2018-3-11 19:25:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这次爬取是爬取250部电影的相关内容,分别用了requests请求url,正则表达式re与BeautifulSoup作为内容过滤

openpyxl作为excel的操作模块,本人为才学不久的新手,代码编写有点无脑和啰嗦,希望有大神能多提建议

    首先,代码清单如下:
  1. import requests  
  2. import re  
  3. from bs4 import BeautifulSoup  
  4. import openpyxl  
  5. def get_movie_top250_name(soup):  
  6.     targets = soup.find_all('span',class_="title")           #用BeautifulSoup找寻一个内容为一个列表  
  7.     targets_name = re.findall(r'.*?title">(.*?)<\/span',str(targets))       #用正则表达式去掉标签  
  8.     for each in targets_name:               #剔除targets_name当中的别名  
  9.         if '\xa0' in each:  
  10.             targets_name.remove(each)  
  11.     return targets_name  
  12.   
  13. def get_movie_top250_workers(soup):  
  14.     targets = soup.find_all('p',class_="")  
  15.     targets_workers = []  
  16.     for each in targets:  
  17.         targets_workers.append(each.text.replace('<p class="">','').replace('\n                            ','').replace('\xa0','').replace('\n                        ',''))  
  18.     return targets_workers  
  19.   
  20. def get_movie_top250_star(soup):  
  21.     targets = soup.find_all('div', class_="star")  
  22.     targets_star = re.findall(r'<span class="rating_num" property="v:average">(.*?)<\/span>',str(targets))  
  23.     return targets_star  
  24. def get_movie_top250_quote(soup):  
  25.     targets = soup.find_all('p', class_="quote")  
  26.     targets_quote = re.findall(r'<span class="inq">(.*?)<\/span>',str(targets))  
  27.     return targets_quote  
  28.   
  29. def save_to_excel(name,workers,star,quote):  
  30.   
  31.     wb = openpyxl.Workbook()  
  32.     ws =wb.active  
  33.   
  34.     ws['A1'] = "电影名称"  
  35.     ws['B1'] = "工作人员"  
  36.     ws['C1'] = "评分"  
  37.     ws['D1'] = "描述"  
  38.     for i in range(len(name)):  
  39.         result = [name[i],workers[i],star[i],quote[i]]  
  40.         ws.append(result)  
  41.     wb.save("豆瓣电影TOP250.xlsx")  
  42. def main():  
  43.     numbers = 1  
  44.     name = []  
  45.     workers = []  
  46.     star = []  
  47.     quote = []  
  48.     result = []  
  49.     while numbers:  
  50.         url = 'https://movie.douban.com/top250?start={}&filter='.format(numbers-1)  
  51.         headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}  
  52.         res = requests.get(url,headers = headers)  
  53.         soup = BeautifulSoup(res.text, "html.parser")  
  54.         name_1 = get_movie_top250_name(soup)  
  55.         workers_1 = get_movie_top250_workers(soup)  
  56.         star_1 = get_movie_top250_star(soup)  
  57.         quote_1 = get_movie_top250_quote(soup)  
  58.         for i in range(len(name_1)):  
  59.             name.append(name_1[i])  
  60.             workers.append(workers_1[i])  
  61.             star.append(star_1[i])  
  62.             quote.append(quote_1[i])  
  63.         numbers += 25  
  64.         if numbers > 250:  
  65.             break  
  66.     save_to_excel(name,workers,star,quote)  
  67. if __name__ == '__main__':  
  68.     main()  
复制代码

运行结果如下:
发博图片1.png
                其实一开始,有想过是不是直接用正则表达式找就行了,不用BeautifulSoup。然后完全是因为自己想多熟悉一下两个的用法,所以都用到了,自认为两个一起用能更准确些。首先谈一谈最重要的是码代码时候遇到的一些问题:

1.抓取电影的名字:
  1. def get_movie_top250_name(soup):  
  2.     targets = soup.find_all('span',class_="title")           #用BeautifulSoup找寻一个内容为一个列表  
  3.     targets_name = re.findall(r'.*?title">(.*?)<\/span',str(targets))       #用正则表达式去掉标签  
  4.     for each in targets_name:               #剔除targets_name当中的别名  
  5.         if '\xa0' in each:  
  6.             targets_name.remove(each)  
  7.     return targets_name  
复制代码

比较容易被卡住的点就是电影名称里有许多的别名,如图:            
发博图片2.png
可以看出,每一个电影的class ='title'有两个,这就不好分辨了。由于本人刚学不久,所以在筛选电影的名字的时候,就花了我半天时间。其实回想很简单,如果把所有的名字print出来,就会发现别名前面都会有字符'\xa0',所以只要做一个if判断的过滤就可以解决(if结果问人才写出来的,要补基础!)。
2.爬取工作人员的内容,其中也包括电影的性质归属了,两个一起抓了,其实我就是懒,没有分开:
  1. def get_movie_top250_workers(soup):  
  2.     targets = soup.find_all('p',class_="")  
  3.     targets_workers = []  
  4.     for each in targets:  
  5.         targets_workers.append(each.text.replace('<p class="">','').replace('\n                            ','').replace('\xa0','').replace('\n                        ',''))  
  6.     return targets_workers  
复制代码

3.抓取评分、描述(一句很骚的话描述了整个电影),都跟第二点差不多,略过

评分

参与人数 2荣誉 +10 鱼币 +10 收起 理由
小甲鱼 + 5 + 5 支持楼主!
°蓝鲤歌蓝 + 5 + 5 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-11 19:42:07 | 显示全部楼层
很厉害,过来学习,加油。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-11 19:52:08 | 显示全部楼层
°蓝鲤歌蓝 发表于 2018-3-11 19:42
很厉害,过来学习,加油。

多得你帮助啊。还要谢谢你哈哈,加油
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-11 19:53:25 | 显示全部楼层
不想呆在桂平 发表于 2018-3-11 19:52
多得你帮助啊。还要谢谢你哈哈,加油

算不上帮忙。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-11 22:48:07 | 显示全部楼层
哈哈,好棒,加油!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-11 22:56:17 | 显示全部楼层
小甲鱼 发表于 2018-3-11 22:48
哈哈,好棒,加油!

甲鱼哥来关顾,荣幸荣幸,多谢支持
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 02:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表