Forward团队-爬虫豆瓣top250项目-模块开发过程

项目托管平台地址:https://github.com/xyhcq/top250 

开发模块功能: 写入文件功能

开发时间:3小时

实现将爬取到的信息写入到文件中的功能

实现过程:

# 打开文件
f=open("top250.txt","w")

在别的队员写的代码基础上,加入功能代码

def getData(html):# 分析代码信息,提取数据soup = BeautifulSoup(html, "html.parser")# 找到第一个class属性值为grid_view的ol标签movieList=soup.find('ol',attrs={'class':'grid_view'})# 找到所有的li标签for movieLi in movieList.find_all('li'):    # 找到第一个class属性值为hd的div标签movieHd=movieLi.find('div',attrs={'class':'hd'})# 找到第一个class属性值为title的span标签 #也可使用.string方法# 获取电影名字movieName=movieHd.find('span',attrs={'class':'title'}).getText()print movieNamef.write('电影名:'+movieName.encode('utf-8')+'    ')# 获取电影链接movieUrl=movieHd.find('a class="" href="')print movieUrl# 写入文件f.write('链接:'+str(movieUrl)+'    ')# 获取电影导演/演员movieBd = movieLi.find('div', attrs={'class': 'bd'})movieSF=movieBd.find('p',attrs={'class':''}).getText()print movieSF# 写入文件f.write('Staff:'+movieSF.encode('utf-8')+'    ')# 获取电影的评分movieScore=movieLi.find('span',attrs={'class':'rating_num'}).getText()print movieScore# 写入文件f.write('评分:'+movieScore.encode('utf-8')+'    ')#获取电影的评论数movieEval=movieLi.find('div',attrs={'class':'star'})movieEvalNum=re.findall(r'\d+',str(movieEval))[-1]print movieEvalNumf.write('评论数:'+movieEvalNum.encode('utf-8')+'    ')# 获取电影短评movieQuote = movieLi.find('span', attrs={'class': 'inq'})# 有的电影没有短评,为防止报错,加次if(movieQuote):print movieQuote.getText()# 写入文件f.write('短评:'+movieQuote.getText().encode('utf-8')+'\n')else:# 写入文件f.write('短评:'+"这个电影没有短评"+'\n')

最后

# 关闭文件,否则容易写入不全    
f.close()

遇到的问题:

刚开始写入文件时会报错,错误提示是不能写入,后来发现文件编码不支持ascii,转换了一下编码 .encode('utf-8') 就正常了

 

转载于:https://www.cnblogs.com/kasumis/p/7739478.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390990.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CSS3 outline-offset 属性 项目中input会遇到

outline在一个声明中设置所有的轮廓属性。outline:颜色(outline-line)样式(outline-style)宽度(outline-width) outline-offset 属性对轮廓进行偏移,并在边框边缘进行绘制。 轮廓在两方面与边框…

回归分析中自变量共线性_具有大特征空间的回归分析中的变量选择

回归分析中自变量共线性介绍 (Introduction) Performing multiple regression analysis from a large set of independent variables can be a challenging task. Identifying the best subset of regressors for a model involves optimizing against things like bias, multi…

winform窗体模板_如何验证角模板驱动的窗体

winform窗体模板介绍 (Introduction) In this article, we will learn about validations in Angular template-driven forms. We will create a simple user registration form and implement some inbuilt validations on it. Along with the inbuilt validations, we will a…

【loj6191】「美团 CodeM 复赛」配对游戏 概率期望dp

题目描述 n次向一个栈中加入0或1中随机1个,如果一次加入0时栈顶元素为1,则将这两个元素弹栈。问最终栈中元素个数的期望是多少。 输入 一行一个正整数 n 。 输出 一行一个实数,表示期望剩下的人数,四舍五入保留三位小数。 样例输入…

查找满足断言的第一个元素

问题:查找满足断言的第一个元素 我刚刚开始使用Java 8的lambdas,我尝试去实现一些我在函数式语言里面经常用的 例如,大部分的函数式语言里有一些查找函数,针对序列或者list进行操作,返回使得断言为真的第一个元素。我…

Lock和synchronized的选择

学习资源:http://www.cnblogs.com/dolphin0520/p/3923167.html 一.java.util.concurrent.locks包下常用的类 1.Lock public interface Lock { void lock();//用来获取锁。如果锁已被其他线程获取,则进行等待。void lockInterruptibly() throws InterruptedException…

python 面试问题_值得阅读的30个Python面试问题

python 面试问题Interview questions are quite tricky to predict. In most cases, even peoples with great programming ability fail to answer some simple questions. Solving the problem with your code is not enough. Often, the interviewer will expect you to hav…

spring boot中 使用http请求

因为项目需求,需要两个系统之间进行通信,经过一番调研,决定使用http请求。服务端没有什么好说的,本来就是使用web 页面进行访问的,所以spring boot启动后,controller层的接口就自动暴露出来了,客…

arduino joy_如何用Joy开发Kubernetes应用

arduino joyLet’s face it: Developing distributed applications is painful.让我们面对现实:开发分布式应用程序很痛苦。 Microservice architectures might be great for decoupling and scalability but they are intimidatingly complex when it comes to de…

怎么样得到平台相关的换行符?

问题:怎么样得到平台相关的换行符? Java里面怎么样得到平台相关的换行符。我不可能到处都用"\n" 回答一 In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting me…

scrapy常用工具备忘

scrapy常用的命令分为全局和项目两种命令,全局命令就是不需要依靠scrapy项目,可以在全局环境下运行,而项目命令需要在scrapy项目里才能运行。一、全局命令##使用scrapy -h可以看到常用的全局命令 [rootaliyun ~]# scrapy -h Scrapy 1.5.0 - n…

机器学习模型 非线性模型_机器学习:通过预测菲亚特500的价格来观察线性模型的工作原理...

机器学习模型 非线性模型Introduction介绍 In this article, I’d like to speak about linear models by introducing you to a real project that I made. The project that you can find in my Github consists of predicting the prices of fiat 500.在本文中,…

NOIP赛前模拟20171027总结

题目: 1.寿司 给定一个环形的RB串要求经过两两互换后RB分别形成两段连续区域,求最少操作次数(算法时间O(n)) 2.金字塔 给定一个金字塔的侧面图有n层已知每一层的宽度高度均为1要求在图中取出恰好K个互不相交的矩形(边缘可以重叠),求最多可以取…

虚幻引擎 js开发游戏_通过编码3游戏学习虚幻引擎4-5小时免费游戏开发视频课程

虚幻引擎 js开发游戏One of the most widely used game engines is Unreal Engine by Epic Games. On the freeCodeCamp.org YouTube channel, weve published a comprehensive course on how to use Unreal Engine with C to develop games.Epic Games的虚幻引擎是使用最广泛的…

建造者模式什么时候使用?

问题:建造者模式什么时候使用? 建造者模式在现实世界里面的使用例子是什么?它有啥用呢?为啥不直接用工厂模式 回答一 下面是使用这个模式的一些理由和Java的样例代码,但是它是由设计模式的4个人讨论出来的建造者模式…

TP5_学习

2017.10.27:1.index入口跑到public下面去了 2.不能使用 define(BIND_MODULE,Admin);自动生成模块了,网上查了下: \think\Build::module(Admin);//亲测,可用 2017.10.28:1.一直不知道怎么做查询显示和全部显示,原来如此简单&#x…

sql sum语句_SQL Sum语句示例说明

sql sum语句SQL中的Sum语句是什么? (What is the Sum statement in SQL?) This is one of the aggregate functions (as is count, average, max, min, etc.). They are used in a GROUP BY clause as it aggregates data presented by the SELECT FROM WHERE port…

10款中小企业必备的开源免费安全工具

10款中小企业必备的开源免费安全工具 secist2017-05-188共527453人围观 ,发现 7 个不明物体企业安全工具很多企业特别是一些中小型企业在日常生产中,时常会因为时间、预算、人员配比等问题,而大大减少或降低在安全方面的投入。这时候&#xf…

为什么Java里面没有 SortedList

问题:为什么Java里面没有 SortedList Java 里面有SortedSet和SortedMap接口,它们都属于Java的集合框架和提供对元素进行排序的方法 然鹅,在我的认知里Java就没有SortedList这个东西。你只能使用java.util.Collections.sort()去排序一个list…

图片主成分分析后的可视化_主成分分析-可视化

图片主成分分析后的可视化If you have ever taken an online course on Machine Learning, you must have come across Principal Component Analysis for dimensionality reduction, or in simple terms, for compression of data. Guess what, I had taken such courses too …