用python写网络爬虫-下载百思不得姐视频

跟着哔哩哔哩视频开始写爬虫啦,目标:下载白死不得姐的视频

1.2.7->3重要改变:

1.import urllib2替换为
import urllib.request
2.html=urllib.request.urlopen(xxx).read()
后要 html=html.decode('UTF-8')
防止出现:TypeError: cannot use a string pattern on a bytes-like object

2.正则表达式

reg=r'data-mp4="(.*?)"'

reg就是正则表达式 r’xxx’:r的目的啊是以原生字符串显示(即:/n不换行而直接显示/n这个字符串) xxx即中间的匹配式子
.?是王能替换变量,在这里表示MP4的网址 ()扩住.?表示把这个替换的变量取出,如果没有括号意思就是只匹配不取出

3.完整代码:

import urllib.request
import urllib.error
import re

print("plz input 1~50")
page=input("which budejie page you want to download?(1~50)")
page=int(page)
while page>50:
    print("plz input 1~50")
    page=input("which budejie page you want to download?(1~50)")
    page=int(page)
headers={'User-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
url="http://www.budejie.com/"+str(page)
request=urllib.request.Request(url,headers=headers)
html=urllib.request.urlopen(request).read()
html=html.decode('UTF-8')#2.7->3!!!!!!!!
reg=r'="(.*?).mp4'
mp4list=re.findall(reg,html)
for i in mp4list:
    print("downloading:"+i+".mp4")
    filename=re.split('/',i)[-1]+".mp4"
    urllib.request.urlretrieve(i+".mp4",filename)

这里正则表达式一开始写的智能爬取一半视频,经过修改,直接找地址.mp4的方式写正则,最后用地址的时候再给加上”.mp4”就可以完美爬取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值