模拟登陆豆瓣

第一次登陆需要验证码,之后的登陆可以隐去 “login(”username’,’password’)”,因为使用session保存了必要的登陆信息,代码如下:

import requests
try:import cookielib
except:import http.cookiejar as cookielib
import re
import time
import os.path
import json
from bs4 import BeautifulSoup
try:from PIL import Image
except:passfrom mywordCloud import save_jieba_result
from mywordCloud import draw_wordcloud
import threading
import codecs
# 构造 Request headers
agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
headers = {"Host": "www.douban.com","Referer": "https://www.douban.com/",'User-Agent': agent,
}#使用cookie登录信息
session=requests.session()
session.cookies=cookielib.LWPCookieJar(filename='cookies')try:session.cookies.load(ignore_discard=True)print('成功加载cookie')
except:print("cookie 未能加载")# 获取验证码
def get_captcha(url):#获取验证码print('获取验证码',url)captcha_url = urlr = session.get(captcha_url, headers=headers)print('test')with open('captcha.jpg', 'wb') as f:f.write(r.content)f.close()# 用pillow 的 Image 显示验证码# 如果没有安装 pillow 到源代码所在的目录去找到验证码然后手动输入try:im = Image.open('captcha.jpg')im.show()im.close()except:print(u'请到 %s 目录找到captcha.jpg 手动输入' % os.path.abspath('captcha.jpg'))captcha = input("please input the captcha\n>")return captchadef isLogin():#登录个人主页,查看是否登录成功url='https://www.douban.com/people/151607908/'login_code=session.get(url,headers=headers,allow_redirects=False).status_codeif login_code==200:return Trueelse:return Falsedef login(acount,secret):douban="https://www.douban.com/"htmlcha=session.get(douban,headers=headers).textpatterncha=r'id="captcha_image" src="(.*?)" alt="captcha"'httpcha=re.findall(patterncha,htmlcha)pattern2=r'type="hidden" name="captcha-id" value="(.*?)"'hidden_value=re.findall(pattern2,htmlcha)print(hidden_value)post_data = {"source": "index_nav",'form_email': acount,'form_password': secret}if len(httpcha)>0:print('验证码连接',httpcha)capcha=get_captcha(httpcha[0])post_data['captcha-solution']=capchapost_data['captcha-id']=hidden_value[0]print (post_data)post_url='https://www.douban.com/accounts/login'login_page=session.post(post_url,data=post_data,headers=headers)#保存cookiessession.cookies.save()if isLogin():print('登录成功')else:print('登录失败')def get_movie_sort():time.sleep(1)movie_url='https://movie.douban.com/chart'html=session.get(movie_url,headers=headers)soup=BeautifulSoup(html.text,'html.parser')result=soup.find_all('a',{'class':'nbg'})print(result)#爬取短评论
def get_comment(filename):  #filename为爬取得内容保存的文件begin=1comment_url = 'https://movie.douban.com/subject/11600078/comments'next_url='?start=20&limit=20&sort=new_score&status=P'headers2 = {"Host": "movie.douban.com","Referer": "https://www.douban.com/",'User-Agent': agent,'Connection': 'keep-alive',}f=open(filename,'w+',encoding='utf-8')while(True):time.sleep(6)html=session.get(url=comment_url+next_url,headers=headers2)soup=BeautifulSoup(html.text,'html.parser')#爬取当前页面的所有评论result=soup.find_all('div',{'class':'comment'}) #爬取得所有的短评pattern4 = r'<p class=""> (.*?)' \r'</p>'for item in result:s=str(item)count2=s.find('<p class="">')count3=s.find('</p>')s2=s[count2+12:count3]  #抽取字符串中的评论if 'class' not in s2:f.write(s2)#获取下一页的链接next_url=soup.find_all('div',{'id':'paginator'})pattern3=r'href="(.*?)">后页'if(len(next_url)==0):breaknext_url=re.findall(pattern3,str(next_url[0]))  #得到后页的链接if(len(next_url)==0): #如果没有后页的链接跳出循环breaknext_url=next_url[0]print('%d爬取下一页评论...'%begin)begin=begin+1#如果爬取了5次则多休息2秒if(begin%6==0):time.sleep(40)print('休息...')print(next_url)f.close()#多线程爬虫,爬取豆瓣影评
def thread_get_comment(filename):next_url = '?start=19&limit=20&sort=new_score&status=P'headers2 = {"Host": "movie.douban.com","Referer": "https://www.douban.com/",'User-Agent': agent,'Connection': 'keep-alive',}f = open(filename, 'w+', encoding='utf-8')comment_url = 'https://movie.douban.com/subject/26363254/comments'crawl_queue=[comment_url+next_url]crawl_queue.append('https://movie.douban.com/subject/26363254/comments?start=144&limit=20&sort=new_score&status=P')seen=set(crawl_queue)def process_queue():begin = 1while True:try:url=crawl_queue.pop()except  IndexError:breakelse:time.sleep(5)html = session.get(url=url,headers=headers2)soup = BeautifulSoup(html.text, 'html.parser')# 爬取当前页面的所有评论result = soup.find_all('div', {'class': 'comment'})  # 爬取得所有的短评pattern4 = r'<p class=""> (.*?)' \r'</p>'for item in result:s = str(item)count2 = s.find('<p class="">')count3 = s.find('</p>')s2 = s[count2 + 12:count3]  # 抽取字符串中的评论f.write(s2)# 获取下一页的链接next_url = soup.find_all('div', {'id': 'paginator'})pattern3 = r'href="(.*?)">后页'if (len(next_url) == 0):breaknext_url = re.findall(pattern3, str(next_url[0]))  # 得到后页的链接if (len(next_url) == 0):  # 如果没有后页的链接跳出循环breaknext_url = next_url[0]print('%d爬取下一页评论...' % begin)begin = begin + 1# 如果爬取了6次则多休息2秒if (begin % 6 == 0):print('休息...')time.sleep(30)print(next_url)if comment_url+next_url not in seen:seen.add(comment_url+next_url)crawl_queue.append(comment_url+next_url)threads=[]max_threads=5while threads or crawl_queue:for thread in threads:if not thread.is_alive():threads.remove(thread)while len(threads)< max_threads and crawl_queue:thread=threading.Thread(target=process_queue)print('--------下一个线程----------')thread.setDaemon(True) # set daemon so main thread can exit when receive ctrl + Cthread.start()threads.append(thread)time.sleep(2)f.close()if __name__=='__main__':if isLogin():print('您已经登录')else:print('xs')login('dsdz@qq.com','5sdfsd6')file_name='key3.txt'get_comment(file_name)        #单线程爬虫#thread_get_comment(file_name)  #多线程爬虫save_jieba_result(file_name)draw_wordcloud('pjl_jieba.txt')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245

爬取得评论保存在key3.txt 文本文件中: 

生成图云

第一步需要:安装必要的Python 库,其中需要的有 生成图云scipy 、wordcloud。python库的安装方法,可以参考笔者的博客安装第三方库。一切准备就绪之后,就可以使用jieba分词对得到的所有评论进行分词,分词时候就可以绘制图云。

其中主要的代码 mywordCloud.py

from scipy.misc import  imread
import codecs
from os import  path
import jieba
from wordcloud import WordCloud#暂时没有用到
def get_all_keywords(file_name):word_lists=[]  #关键词列表with codecs.open(file_name,'r',encoding='utf-8') as f:Lists=f.readlines()for li in Lists:cut_list=list(jieba.cut(li))for word in cut_list:word_lists.append(word)word_lists_set=set(word_lists)  #去除相同的元素sort_count=[]word_lists_set=list(word_lists_set)length=len(word_lists_set)print(u'共有%d个关键词'%length)k = 1for w in word_lists_set:sort_count.append(w + u':' + str(word_lists.count(w)) + u"次\n")print(u"%d---" % k + w + u":" + str(word_lists.count(w)) + u"次")k += 1with codecs.open('count_word.txt', 'w', encoding='utf-8') as f:f.writelines(sort_count)def save_jieba_result(file_name):#设置多线程切割#jieba.enable_parallel(4)dirs=path.join(path.dirname(__file__),file_name)print(dirs)with codecs.open(dirs,encoding='utf-8') as f:comment_text=f.read()cut_text=" ".join(jieba.cut(comment_text))with codecs.open('pjl_jieba.txt','w',encoding='utf-8') as f:f.write(cut_text)def draw_wordcloud(file_name):with codecs.open(file_name,encoding='utf-8') as f:comment_text=f.read()color_mask=imread('timg.jpg') #读取背景图片stopwords = ['png','douban','com','href','https','img','img3','class','source','icon','shire',u'有点',u'真的',u'觉得',u'还是',u'一个',u'就是', u'电影', u'你们', u'这么', u'不过', u'但是', u'什么', u'没有', u'这个', u'那个', u'大家', u'比较', u'看到', u'真是',u'除了', u'时候', u'已经', u'可以']font = r'C:\Windows\Fonts\simfang.ttf'cloud=WordCloud(font_path=font,background_color='white',max_words=20000,max_font_size=200,min_font_size=10,mask=color_mask,stopwords=stopwords)word_cloud=cloud.generate(comment_text)  #产生词云word_cloud.to_file('mycloud.jpg')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

通过上面两个代码,就可以生成漂亮的图云,来预测观看《战狼2》这部电影的人主要评论的关键词: 

附上笔者的github源代码地址:https://github.com/wu-yy/warWolf

【干货】python爬取《战狼2》电影短评论,生成图云相关推荐

  1. python爬取豆瓣影评生成词云的课程设计报告_简单爬取《小丑》电影豆瓣短评生成词云...

    导语 在前段时间看了杰昆菲尼克斯的小丑电影,心里很好奇大部分观众看完这部电影之后对此有什么评价,然后看了看豆瓣短评之后,觉得通过python把短评中出现最多的单词提取出来,做成一张词云,看看这部电影给 ...

  2. Python爬取你好李焕英豆瓣短评生成词云

    爬取过程: 你好,李焕英 短评的URL: https://movie.douban.com/subject/34841067/comments?start=20&limit=20&st ...

  3. Python 爬取B站视频信息 弹幕信息 生成词云统计

    本文介绍功能:实现爬取B站视频信息(用户输入关键词).爬取弹幕信息(支持自定义天数).生成词云图 完整代码地址:https://github.com/736755244/py_bilibili 一.数 ...

  4. Python爬取视频之爱情电影及解密TS文件和两种合并ts的方法

    俗话说,兴趣所在,方能大展拳脚.so结合兴趣的学习才能事半功倍,更加努力专心,apparently本次任务是在视频网站爬取一些好看的小电影,地址不放(狗头保命)只记录过程. 实现功能: 从网站上爬取采 ...

  5. Python爬取2022春节档电影信息

    Python爬取2022春节档电影信息 前提条件 相关介绍 实验环境 具体步骤 确定目标网站 分析网站 按F12打开浏览器操作台 按Ctrl+Shift+C快捷键,用鼠标找到目标元素 按Ctrl+F快 ...

  6. Python爬取豆瓣热映电影

    Python爬取豆瓣热映电影 # encoding: utf-8import requests from lxml import etree# 1. 将目标网站上的页面抓取下来 headers = { ...

  7. Python爬取豆瓣《复仇者联盟3》评论并生成乖萌的格鲁特

    代码地址如下: http://www.demodashi.com/demo/13257.html 1. 需求说明 本项目基于Python爬虫,爬取豆瓣电影上关于复仇者联盟3的所有影评,并保存至本地文件 ...

  8. python爬取二手房库存,存数据库,生成折线图(下)

    python爬取二手房库存,存数据库,生成折线图(下) 数据库有了房价的多阶段价格后,即可生成折线图.默认我的数据库已经有很多天的数据了 进入html页面的时候,默认加载房价走势图 在vue 的mou ...

  9. python爬取《三国演义》小说统计词频生成词云图

    python爬取<三国演义>小说&统计词频&生成词云图 注意点: 爬取小说正文时用的正则表达式涉及到多行匹配.需要开启多行模式(?s) book_content_re = ...

  10. 用python爬取豆瓣影评及影片信息(评论时间、用户ID、评论内容)

    爬虫入门:python爬取豆瓣影评及影片信息:影片评分.评论时间.用户ID.评论内容 思路分析 元素定位 完整代码 豆瓣网作为比较官方的电影评价网站,有很多对新上映影片的评价,不多说,直接进入正题. ...

最新文章

  1. tcpdump抓取ipip报文
  2. python使用符号 表示单行注释-Python注释(多行注释和单行注释)用法详解
  3. C、C++中的逻辑运算符
  4. [ROBOT] python library 如何能获取到ROBOT框架里面的全局变量,例如${OUTPUT DIR}等
  5. 关于 SAP UI5 Table 控件中行合并的实现方式
  6. requirejs与anjularjs框架
  7. 微信开发 getUserInfo:fail tunneling socket could not be established, cause=connect ECONNREFUSED
  8. (转)淘淘商城系列——内容管理
  9. linux安装 grub失败,安装linux+windows的系统 如果grub引导失败的解决方法
  10. POJ NOI0105-43 质因数分解
  11. VGA、DVI、HDMI都是什么意思?
  12. ps添加的阴影怎么去除_ps怎么可以把阴影去除
  13. “非著名相声演员”郭德纲【ZZ】
  14. 邬先生及时功成身退,是明哲保身的聪明做法 --- 我看电视剧《雍正王朝》
  15. 日晒、水洗、蜜处理?简单带你认识咖啡加工处理法
  16. 安卓手机变成横屏_学会用手机远程控制电脑,出门在外,随时随地也能轻松办公!...
  17. 柔性传感器产业化将至或将成为折叠屏背后的“黑科技”
  18. 综述 | 实例分割研究
  19. 1990-2021年汇率年平均价数据
  20. 表单验证的相关事件和辅助特效

热门文章

  1. MySQL字符集是什么
  2. hiveSQL执行计划(explain使用全网最详细!!)
  3. Low-poly低面建模(低像素多边形)
  4. 锐龙r75800u参数 r7 5800u怎么样
  5. DM368开发 -- 再论 UBL
  6. Crystal Reports(水晶报表)安装及拉(PULL)模式/推(PUSH)模式的使用
  7. docker xware下载慢_【原创】基于Docker实现迅雷远程下载
  8. 计算机硬盘有磁性材料吗,电脑硬盘里有磁铁吗
  9. 随身计算机的硬盘是该换了,手提电脑硬盘可以换吗
  10. 10大动图:秒懂各种常用通信协议原理