戴正平 發表於 2019-5-13 15:45:00

python爬虫(爬取视频)

<h1 id="爬虫爬视频">爬虫爬视频</h1>
<h2 id="爬取步骤">爬取步骤</h2>
<p>第一步:获取视频所在的网页</p>
<p>第二步:F12中找到视频真正所在的链接</p>
<p>第三步:获取链接并转换成二进制</p>
<p>第四部:保存</p>
<h2 id="保存步骤代码">保存步骤代码</h2>
<pre><code class="language-python">import re
import requests
response =requests.get('https://vd4.bdstatic.com/mda-jcrx64vi5vct2d2u/sc/mda-jcrx64vi5vct2d2u.mp4?auth_key=1557734214-0-0-d6a29a90222c6caf233e8a2a34c2e37a&amp;bcevod_channel=searchbox_feed&amp;pd=bjh&amp;abtest=all')
video = response.content         #把文件保存成二进制
with open(r'D:\图片\绿色.mp4','wb') as fw:
    fw.write(video)         #将文件内容写入该文件
    fw.flush()               #刷新
</code></pre>
<h2 id="爬酷6首页的所有视频">爬酷6首页的所有视频</h2>
<pre><code class="language-python">#有点偷懒变量名用简单字母啦.............
# https://www.ku6.com/index
# &lt;a class="video-image-warp" target="_blank" href="(.*?)"&gt;
#this.src({type: "video/mp4", src: "(.*?)"})
#src({type: "video/mp4", src: "(.*?)"})
import re# 载入模块
import requests# 载入模块
new_list = []
time = 0
response = requests.get('https://www.ku6.com/index')
data = response.text
# print(data)
url = re.findall('&lt;a class="video-image-warp" target="_blank" href="(.*?)"&gt;',data)
for a in url : #type:str
    if a.startswith('/v') or a.startswith('/d'):
      new_list.append(f'https://www.ku6.com{a}')
    elif a.startswith('ht'):
      new_list.append(f"{a.split('垃')}")
for url_1 in new_list:
    response_1 = requests.get(url_1)
    data_1 = response_1.text
    video = re.findall('&lt;source src="(.*?)" type="video/mp4"&gt;',data_1) or re.findall('type: "video/mp4", src: "(.*?)"',data_1)
    video_1 = video
    x = video_1.split('/')[-1]
    name = f'{x}.mp4'
    video_response = requests.get(video_1)
    video_3 = video_response.content
    with open(f'D:\图片\{name}','wb') as fw:
      fw.write(video_3)
      fw.flush()
      time += 1
      print(f'已经爬取{time}个视频')
</code></pre><br><br>
来源:https://www.cnblogs.com/pythonywy/p/10857032.html
頁: [1]
查看完整版本: python爬虫(爬取视频)