1. 图片代码
import urllib.request
import re
for page in range(254,256):
req = urllib.request.Request("http://jandan.net/ooxx/page-%s#comments"%page) #请求
req.add_header("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3226.400 QQBrowser/9.6.11681.400")
for i in re.findall(r'img src="(.*?)"',urllib.request.urlopen(req).read().decode('utf-8')): #匹配
j = "http:"+ i
file_name =i.split("/")[-1]
urllib.request.urlretrieve(j,'正则下载/%s'%file_name) #下载的方法
2. 视频代码详解
import urllib.request
import re
def getvideo(page):
req = urllib.request.Request("http://www.budejie.com/video/%s"%page) #请求
req.add_header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3226.400 QQBrowser/9.6.11681.400")
html = urllib.request.urlopen(req).read().decode('utf-8') #响应与解码
reg = r'data-mp4="(.*?)"'
for i in re.findall(reg,html): #匹配
#print('正在下载:%s'% i)
file_name =i.split("/")[-1]
urllib.request.urlretrieve(i,'爬虫视频/%s'%file_name) #下载的方法
for i in range(1,11):
getvideo(i)