python爬取B站专栏内高清原图
第一次发帖,多多包涵如果有什么地方做的不好,请指出来,轻喷{:10_254:}
倒数第二行url是专栏的网址,如果想爬取别的专栏,可以更改url
import requests
from bs4 import BeautifulSoup
import re
import os
os.makedirs(r'E://图片')
header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47'}
def getdata(url,header):
req = requests.get(url,headers=header)
imgurls = BeautifulSoup(req.text,"html.parser").find_all('img')
return imgurls
def find_img(urls,header):
i = 0
for each in urls:
i += 1
t = str(each)
url0 = re.findall(r'data-src="(.*)height',t)
if len(url0) != 0:
url = 'https:' + url0[:-2]
print(url)
img_data = requests.get(url,headers = header).content
f = open(r'E://图片/贞德{0}.jpg'.format(i),'wb') #“贞德i.jpg”是图片文件名
f.write(img_data)
f.close()
return 0
url = 'https://www.bilibili.com/read/cv1705485'
find_img(getdata(url,header),header)
页:
[1]