python爬虫大作业任务书_爬虫大作业

通过 爬取2345电影网的电影信息 ,通过电影的类型和评分分别生成相应的词云 进行数据分析

一、准备过程

首先打开2345电影网的热播电影区,网址是https://dianying.2345.com/list/------.html

1022948-20180422113040198-1196877379.png

在这里可以通过审查模式看到第一页的详细信息,而目的则是通过爬取热播页面的每个电影的评分 和 类型来分析最近影迷的观影需求

环境如下:

python3.6.2    PyCharm

Windows7  第三方库(jieba,wordcloud,bs4,Requests,re,wordcloud)

二、代码

1.用requests库和BeautifulSoup库,爬取电影网当前页面的每个电影的电影名、评分、简介、地区、年代、链接等,将获取电影详情的代码定义成一个函数 def getNewDetail(newsUrl):

importrequestsimportrefrom bs4 importBeautifulSoupfrom datetime importdatetimeimportpandas

new={}

defgetNewsDetail(newsUrl):

resd=requests.get(newsUrl)

resd.encoding= 'gb2312'soupd= BeautifulSoup(resd.text, 'html.parser')print(newsUrl)

news={}

news['title']=soupd.select('.tit')[0].select('h1')[0].text #标题

news['emSorce']=float(soupd.select('.tit')[0].select('.pTxt')[0].text.split('\ue60e')[0].rstrip('分')) #评分

#s=soupd.select('.li_4')[1].text.split()[1:]

#if(len(s)>0):

#str=''

#for i in s:

#str=str+i

#print(str)

#writeNewsDetail(str)

news['content:']=soupd.select('.sAll')[0].text.strip() #内容

s = soupd.select('.wholeTxt')[0].text

news['area'] = s[s.find('地区:'):s.find('语言:')].split()[1] #地区

news['tit']=soupd.select('.li_4')[3].text.strip().split()[1] #年代

if len(soupd.select('.li_4')[3].text)>0:

news['tit']=soupd.select('.li_4')[3].text.strip().split()[1]else:

news['tit']=='none'new[soupd.select('.tit')[0].select('h1')[0].text ]= news['emSorce']return news

2.取出一个电影列表页的全部电影 包装成函数def getListPage(pageUrl):

defgetListPage(pageUrl):

res=requests.get(pageUrl)

res.encoding= 'gb2312'soup= BeautifulSoup(res.text, 'html.parser')

newslist=[]for i in soup.select('.v_tb')[0].select(

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值