爱奇艺共享网站源码分享,爱奇艺资源共享

大家好,爱奇艺共享网站源码分享相信很多的网友都不是很明白,包括爱奇艺资源共享也是一样,不过没有关系,接下来就来为大家分享关于爱奇艺共享网站源码分享和爱奇艺资源共享的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

今天讲解如何用python爬取芒果TV、腾讯视频、B站、爱奇艺、知乎、微博这几个常见常用的影视、舆论平台的弹幕和评论,这类爬虫得到的结果一般用于娱乐、舆情分析,如:新出一部火爆的电影,爬取弹幕评论分析他为什么这么火;微博又出大瓜,爬取底下评论看看网友怎么说,等等这娱乐性分析。

本文爬取一共六个平台,十个爬虫案例,如果只对个别案例感兴趣的可以根据:芒果TV、腾讯视频、B站、爱奇艺、知乎、微博这一顺序进行拉取观看。完整的实战源码已在文中,我们废话不多说,下面开始操作!

芒果TV

本文以爬取电影《悬崖之上》为例,讲解如何爬取芒果TV视频的弹幕和评论!

网页地址:

https://www.mgtv.com/b/335313/12281642.html?fpa=15800&fpos=8&lastp=ch_movie\n

弹幕

分析网页

弹幕数据所在的文件是动态加载的,需要进入浏览器的开发者工具进行抓包,得到弹幕数据所在的真实url。当视频播放一分钟它就会更新一个json数据包,里面包含我们需要的弹幕数据。

得到的真实url:

https://bullet-ali.hitv.com/bullet/2021/08/14/005323/12281642/0.json\nhttps://bullet-ali.hitv.com/bullet/2021/08/14/005323/12281642/1.json\n

可以发现,每条url的差别在于后面的数字,首条url为0,后面的逐步递增。视频一共120:20分钟,向上取整,也就是121条数据包。

实战代码

importrequests\nimportpandasaspd\n\nheaders={\n&39;:&39;\n}\ndf=pd.DataFrame()\nforeinrange(0,121):\nprint(f&39;)\nresposen=requests.get(f&39;,headers=headers)\n39;data&39;items&39;ids&用户id\ncontent=i[&39;]39;time&弹幕发生时间\n39;v2_up_count&39;&39;ids&39;弹幕&39;发生时间&39;悬崖之上.csv&39;utf-8&39;user-agent&39;Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/91.0.4472.124Safari/537.36&39;https://comment.mgtv.com/v4/comment/getCommentList?page={o}&subjectType=hunantv2014&subjectId=12281642&_support=10000000&39;data&39;list&39;user&39;nickName&用户昵称\npraiseNum=i[&39;]39;date&发送日期\ncontent=i[&39;]39;nickName&39;praiseNum&39;date&39;content&39;悬崖之上.csv&39;utf-8&39;User-Agent&39;Googlebot&初始为15,7245为视频秒长,链接以三十秒递增\ndf=pd.DataFrame()\nforiinrange(15,7245,30):\nurl=&34;.format(i)\nhtml=requests.get(url,headers=headers).json()\ntime.sleep(1)\nforiinhtml[&39;]:\ncontent=i[&39;]\nprint(content)\ntext=pd.DataFrame({&39;:[content]})\ndf=pd.concat([df,text])\ndf.to_csv(&39;,encoding=&39;,index=False)\n

结果展示:

评论

分析网页

腾讯视频评论数据在网页底部,依然是动态加载的,需要按下列步骤进入开发者工具进行抓包:

点击查看更多评论后,得到的数据包含有我们需要的评论数据,得到的真实url:

https://video.coral.qq.com/varticle/6655100451/comment/v2?callback=_varticle6655100451commentv2&orinum=10&oriorder=o&pageflag=1&cursor=0&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=132&_=1628948867522https://video.coral.qq.com/varticle/6655100451/comment/v2?callback=_varticle6655100451commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6786869637356389636&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=132&_=1628948867523

url中的参数callback以及_删除即可。重要的是参数cursor,第一条url参数cursor是等于0的,第二条url才出现,所以要查找cursor参数是怎么出现的。经过我的观察,cursor参数其实是上一条url的last参数:

实战代码

importrequests\nimportpandasaspd\nimporttime\nimportrandom\n\nheaders={\n&39;:&39;\n}\ndf=pd.DataFrame()\na=1\n281为参照数据包中的oritotal,数据包中一共10条数据,循环280次得到2800条数据,但不包括底下回复的评论\n39;https://video.coral.qq.com/varticle/6655100451/comment/v2?orinum=10&oriorder=o&pageflag=1&cursor=0&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=132&39;https://video.coral.qq.com/varticle/6655100451/comment/v2?orinum=10&oriorder=o&pageflag=1&cursor={cursor}&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=132&39;data&39;last&39;data&39;oriCommList&39;id&39;time&39;up&39;content&39;\\n&39;&39;ids&39;times&39;up&39;content&39;革命者_评论.csv&39;utf-8&34;cookie&34;你的cookie&34;user-agent&34;Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/88.0.4324.104Safari/537.36&39;https://api.bilibili.com/x/v2/dm/history/index?type=1&oid={oid}&month={month}&39;data&拿到所有日期\nprint(list_data)\nfordatainlist_data:\nurls=f&39;\ntext=re.findall(&34;,data_resposen(urls).text)\nforeintext:\nprint(e)\ndata=pd.DataFrame({&39;:[e]})\ndf=pd.concat([df,data])\ndf.to_csv(&39;,encoding=&39;,index=False,mode=&39;)\n\nif__name__==&39;:\noid=&39;39;2021-08&开始日期\nmain(oid,month)\n

结果展示:

评论

分析网页

B站视频的评论内容在网页下方,进入浏览器的开发者工具后,只需要向下拉取即可加载出数据包:

得到真实url:

https://api.bilibili.com/x/v2/reply/main?callback=jQuery1720034332372316460136_1629011550479&jsonp=jsonp&next=0&type=1&oid=589656273&mode=3&plat=1&_=1629012090500\nhttps://api.bilibili.com/x/v2/reply/main?callback=jQuery1720034332372316460136_1629011550483&jsonp=jsonp&next=2&type=1&oid=589656273&mode=3&plat=1&_=1629012513080\nhttps://api.bilibili.com/x/v2/reply/main?callback=jQuery1720034332372316460136_1629011550484&jsonp=jsonp&next=3&type=1&oid=589656273&mode=3&plat=1&_=1629012803039\n

两条urlnext参数,以及_和callback参数。_和callback一个是时间戳,一个是干扰参数,删除即可。next参数第一条为0,第二条为2,第三条为3,所以第一条next参数固定为0,第二条开始递增;网页数据格式为json格式。

实战代码

importrequests\nimportpandasaspd\n\ndf=pd.DataFrame()\nheaders={\n&39;:&39;}\ntry:\na=1\nwhileTrue:\nifa==1:\n39;https://api.bilibili.com/x/v2/reply/main?&jsonp=jsonp&next=0&type=1&oid=589656273&mode=3&plat=1&39;https://api.bilibili.com/x/v2/reply/main?&jsonp=jsonp&next={a}&type=1&oid=589656273&mode=3&plat=1&39;data&39;replies&39;member&39;uname&用户名称\nsex=i[&39;][&39;]39;mid&用户id\ncurrent_level=i[&39;][&39;][&39;]39;content&39;message&39;\\n&39;&用户评论\nlike=i[&39;]39;ctime&评论时间\ndata=pd.DataFrame({&39;:[uname],&39;:[sex],&39;:[mid],\n&39;:[current_level],&39;:[message],&39;:[like],\n&39;:[ctime]})\ndf=pd.concat([df,data])\na+=1\nexceptExceptionase:\nprint(e)\ndf.to_csv(&39;,encoding=&39;)\nprint(df.shape)\n

结果展示,获取的内容不包括二级评论,如果需要,可自行爬取,操作步骤差不多:

爱奇艺

本文以爬取电影《哥斯拉大战金刚》为例,讲解如何爬爱奇艺视频的弹幕和评论!

网页地址:

https://www.iqiyi.com/v_19rr0m845o.html\n

弹幕

分析网页

爱奇艺视频的弹幕依然是要进入开发者工具进行抓包,得到一个br压缩文件,点击可以直接下载,里面的内容是二进制数据,视频每播放一分钟,就加载一条数据包:

得到url,两条url差别在于递增的数字,60为视频每60秒更新一次数据包:

https://cmts.iqiyi.com/bullet/64/00/1078946400_60_1_b2105043.br\nhttps://cmts.iqiyi.com/bullet/64/00/1078946400_60_2_b2105043.br\n

br文件可以用brotli库进行解压,但实际操作起来很难,特别是编码等问题,难以解决;在直接使用utf-8进行解码时,会报以下错误:

UnicodeDecodeError:&39;codeccan&34;utf-8&34;ignore&解压\n\ndf=pd.DataFrame()\nforiinrange(1,23):\nurl=f&39;\nbulletold=requests.get(url).content39;utf-8&解压解码\nwithopen(f&39;,&39;,encoding=&39;)asf:39;./{i}.html&39;rb&读取html文件\nhtml=etree.HTML(html)39;/html/body/danmu/data/entry/list/bulletinfo&39;&39;./contentid/text()&39;&39;./content/text()&39;&39;./likecount/text()&39;contentid&39;content&39;likeCount&39;哥斯拉大战金刚.csv&39;utf-8&39;User-Agent&39;Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/91.0.4472.124Safari/537.36&39;https://sns-comment.iqiyi.com/v3/comment/get_comments.action?agent_type=118&agent_version=9.11.5&business_type=17&content_id=1078946400&page_size=10&从id_list中得到上一条页内容中的最后一个id值\nurl=f&39;\nprint(url)\nres=requests.get(url,headers=headers).json()\nid_list=[]39;data&39;comments&39;id&39;userInfo&39;uname&39;addTime&39;content&39;不存在&用get提取是为了防止键值不存在而发生报错,第一个参数为匹配的key值,第二个为缺少时输出\ntext=pd.DataFrame({&39;:[ids],&39;:[uname],&39;:[addTime],&39;:[content]})\ndf=pd.concat([df,text])\na+=1\ntime.sleep(random.uniform(2,3))\nexceptExceptionase:\nprint(e)\ndf.to_csv(&39;,mode=&39;,encoding=&39;,index=False)\n

结果展示:

知乎

本文以爬取知乎热点话题《如何看待网传腾讯实习生向腾讯高层提出建议颁布拒绝陪酒相关条令?》为例,讲解如爬取知乎回答!

网页地址:

https://www.zhihu.com/question/478781972\n

分析网页

经过查看网页源代码等方式,确定该网页回答内容为动态加载的,需要进入浏览器的开发者工具进行抓包。进入Noetwork→XHR,用鼠标在网页向下拉取,得到我们需要的数据包:

得到的真实url:

https://www.zhihu.com/api/v4/questions/478781972/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_recognized%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cvip_info%2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled&limit=5&offset=0&platform=desktop&sort_by=default\nhttps://www.zhihu.com/api/v4/questions/478781972/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_recognized%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cvip_info%2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled&limit=5&offset=5&platform=desktop&sort_by=default\n

url有很多不必要的参数,大家可以在浏览器中自行删减。两条url的区别在于后面的offset参数,首条url的offset参数为0,第二条为5,offset是以公差为5递增;网页数据格式为json格式。

实战代码

importrequests\nimportpandasaspd\nimportre\nimporttime\nimportrandom\n\ndf=pd.DataFrame()\nheaders={\n&39;:&39;\n}\nforpageinrange(0,1360,5):\nurl=f&39;\nresponse=requests.get(url=url,headers=headers).json()\ndata=response[&39;]\nforlist_indata:\nname=list_[&39;][&39;]39;author&39;id&作者id\ncreated_time=time.strftime(&34;,time.localtime(list_[&39;]))39;voteup_count&赞同数\ncomment_count=list_[&39;]39;content&回答内容\ncontent=&39;.join(re.findall(&34;,content))39;|&39;知乎作者&39;作者id&39;回答时间&39;赞同数&39;底下评论数&39;回答内容&39;知乎回答.csv&39;utf-8&39;User-Agent&39;Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/38.0.2125.122UBrowser/4.0.3214.0Safari/537.36&39;https://m.weibo.cn/detail/4669040301182509&微博爬取大概几十页会封账号的,而通过不断的更新cookies,会让爬虫更持久点…cookie=[cookie.valueforcookieinresposen.cookies]登录后的cookie,SUB用登录后的&39;:f&39;,&39;:&39;,&39;:&39;}ifa==1:url=&39;else:url=f&39;html=requests.get(url=url,headers=headers).json()data=html[&39;]max_id=data[&39;]39;max_id_type&39;data&39;user&39;screen_name&39;user&39;id&39;like_count&点赞数created_at=i[&39;]39;<[^>]*>&39;&39;text&评论print(text)data_json=pd.DataFrame({&39;:[screen_name],&39;:[i_d],&39;:[like_count],&39;:[created_at],&39;:[text]})df=pd.concat([df,data_json])time.sleep(random.uniform(2,7))a+=1exceptExceptionase:print(e)df.to_csv(&39;,encoding=&39;,mode=&39;,index=False)print(df.shape)

结果展示:

以上便是今天的全部内容了,如果你喜欢今天的内容,希望你能在下方点个赞和在看支持我,谢谢!

关于爱奇艺共享网站源码分享,爱奇艺资源共享的介绍到此结束,希望对大家有所帮助。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平