豆瓣有个「请不要害羞」小组,里面每天有很多人自拍,估计很多狼友都是看过的,有时候尺度还是蛮大的。今天突发奇想,能不能写个脚本,把这些图片批量下载下来呢。
于是我就尝试用Python去写了这么个脚本,大概花了30分钟把,可以按照分页,去下载这些MM们分享的图片。但是缺点还是有的,没办法区分是男是女,哈哈,所以,有收藏爱好的朋友,需要自己去归类了。
其实脚本也挺简单,有Python环境的(3.x)可以直接保存脚本,如 download.sh,然后执行「./download (Linux和Mac)」。如果有的狼友有其他需求,可以稍微改一改,去抓取别的。如果豆瓣有限制的话,更可以增加模拟cookie的功能,等你玩(脚本在最后面)。
然后分享一下我之前抓取到的一些图片,希望得到大家的鼓励。
以下是 Python 脚本,有 Python3 环境的可以尝试一下,有兴趣学习的也可以去玩玩,绝对不会损害您的电脑。
複製代碼
#!/usr/bin/env python3
import requests,re,json,html2text,sys,time
from bs4 import BeautifulSoup
import time
from urllib.request import urlretrieveurl=\"https://www.douban.com/group/haixiuzu/\"
def getTopicList():
#循环分页抓取 括号里的换成需要抓取的页数区间,从0开始,间隔越大,时间越长
for x in range(16,20):
page = x * 25
get_url = requests.get(url+\"discussion?start=\"+str(page))
soup = BeautifulSoup(get_url.text,\"html.parser\")
tdList = soup.find_all(\"td\",class_='title')
for i in tdList:
title = i.a.get(\"title\")
if len(i.contents) > 1:
#因为大家晒照片都喜欢在前面加一个【晒】
if '晒' in title:
i_href = i.a.get('href')
getTopicContext(i_href)
time.sleep(1)
#获取帖子里的内容
def getTopicContext(topicUrl):
url = requests.get(topicUrl)
soup = BeautifulSoup(url.text,\"html.parser\")
topicDiv = soup.find_all(\"div\",class_='topic-figure cc')
for div in topicDiv:
if len(div.contents) > 1:
img = div.img
saveImage(img.get(\"src\"))#下载图片到本地
def saveImage(imgUrl):
fileName = imgUrl[imgUrl.rfind(\"/\")+1:]
path = r\"你的路径\"+fileName
urlretrieve(imgUrl,path)if __name__==\"__main__\":
getTopicList()
BB姬
