Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web scraping 网络爬网:从“统计”选项卡获取Youtube视频的共享_Web Scraping_Youtube_Youtube Api_Web Crawler_Extract_Python_Beautifulsoup_Python Requests_Bash_Curl_Sed - Fatal编程技术网

Web scraping 网络爬网:从“统计”选项卡获取Youtube视频的共享

Web scraping 网络爬网:从“统计”选项卡获取Youtube视频的共享,web-scraping,youtube,youtube-api,web-crawler,extract,python,beautifulsoup,python-requests,bash,curl,sed,Web Scraping,Youtube,Youtube Api,Web Crawler,Extract,Python,Beautifulsoup,Python Requests,Bash,Curl,Sed,有人知道一种获得youtube视频(不是我的)份额的方法吗?我想将它们存储到数据库中。它不适用于yt api。另一个问题是,并非所有yt视频都有“统计”选项卡 到目前为止,我尝试了Youtube API、jsoup HTML解析器(显示共享的div不在那里,尽管它是通过firefox中的inspect显示的)和import.io demo,它们都能正常工作,但肯定太贵了 最好的方法是查看网络日志,在这种情况下,它会在以下位置显示一个POST: https://www.youtube.com/in

有人知道一种获得youtube视频(不是我的)份额的方法吗?我想将它们存储到数据库中。它不适用于yt api。另一个问题是,并非所有yt视频都有“统计”选项卡

到目前为止,我尝试了Youtube API、jsoup HTML解析器(显示共享的div不在那里,尽管它是通过firefox中的inspect显示的)和import.io demo,它们都能正常工作,但肯定太贵了


最好的方法是查看网络日志,在这种情况下,它会在以下位置显示一个
POST

https://www.youtube.com/insight_ajax?action_get_statistics_and_data=1&v=$video_id
它在正文中发送XSRF令牌,该令牌在视频页面的原始html正文中可用
https://www.youtube.com/watch?v=javascript对象中的$video_id
,如:

yt.setConfig({
‘XSRF_令牌’:“quffluhqbnnvzux4thr3ev80dhlacv9trkrxc2nwjlxqxxbq3jtc0ttd0jlwennmjdyne5irwhibe9zddjtsk1amktdr5d3jjsnkzvctqwwwwwww3x0tzzvzfdjnp3x0tsox0tsox0tzzzvxzfjpzfjnpg5p5p5vw5v2w2wxxxxxxxbjjzbjjzzp6jzzk6jj6jjc5j5j5j5j2tj2ftjjjjjj2vwjjjjjjjjjj,
“XSRF_字段_名称”:“会话_令牌”,
“XSRF_重定向_令牌”:“VLHMKN6F56DGGYCM4RG7JCZR0VJ8MTQ5ODA1NZIWMKAXDK3OTCWODAY”
});
它还需要在同一视频页面中设置一些cookies

使用 与&:

导入请求
从bs4导入BeautifulSoup
进口稀土
s=请求。会话()
video_id=“CPkU0dF4JKo”
r=s.get('https://www.youtube.com/watch?v={}.格式(视频id))
xsrf\u token=re.search(“'xsrf\u token'\s*:\s*\”(.*)\”,r.text,re.IGNORECASE)。组(1)
r=s.post(
'https://www.youtube.com/insight_ajax?action_get_statistics_and_data=1&v={}格式(视频id),
数据={
“会话_令牌”:xsrf_令牌
}
)
指标=[
int(t.text.encode('ascii','ignore').split('',1)[0])
对于美化组中的t(r.content,“lxml”).find('html_content').find(“tr”).findAll(“div”,{“class”:“bragbar metric”})
]
打印(度量)
使用 与&:

以下bash脚本将:

  • 请求视频页面
    https://www.youtube.com/watch?v=$video_id
  • 将cookie存储在名为
    cookie.txt的文件中
  • 在以下请求中使用提取被称为
    session\u令牌的
    XSRF\u令牌
  • 请求视频统计页面
    https://www.youtube.com/insight_ajax?action_get_statistics_and_data=1&v=$video_id
    与以前存储的Cookie
  • 解析xml结果提取
    CDATA
    部分
  • 使用解析html以提取
    bragbar度量
    class div,并使用
    json{}
  • 用于删除unicode字符
剧本:

video_id=CPkU0dF4JKo
会话\u令牌=$(curl-s-c cookie.txt)https://www.youtube.com/watch?v=$video_id“|\
sed-rn“s/*'XSRF\u令牌'\s*:\s*\'(.*)\“*/\1/p”)
curl-s-b cookie.txt-d“会话\u令牌=$session\u令牌”\
"https://www.youtube.com/insight_ajax?action_get_statistics_and_data=1&v=$video_id“|\
xml_grep——仅限文本的“html_内容”|\
pup'div table tr.bragbar公制文本{}'|\
sed的/\xc2\x91\|\xc2\x92\|\xc2\xa0\|\xe2\x80\x8e/'|\
sed's/\s.*$/'
它提供了视图数、观看时间、订阅数和共享数:

120862
454
18
213

要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题对于堆栈溢出来说是离题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作。