使用GitHub上的Python脚本自动爬取Bing壁纸并上传至百度网盘
要使用GitHub上的Python自动爬取Bing壁纸并上传到百度网盘,首先需要安装相关库,如`requests`、`BeautifulSoup`和`baidupcsapi`。然后,编写一个Python脚本,通过调用Bing壁纸API获取壁纸图片链接,使用BeautifulSoup解析网页内容,提取图片链接。接着,使用requests库下载图片并保存到本地。最后,使用baidupcsapi库将图片上传到百度网盘。整个过程可以通过定时任务实现每天自动执行。
importrequestsimportbase64fromdatetimeimportdatetimefromdatetimeimporttimedeltafromdatetimeimporttimezonefrombs4importBeautifulSoupimporttimeimportddddocrimportosbaidu_cookie=os.environ["BAIDU_COOKIE"]SHA_TZ=timezone(timedelta(hours=8),name='Asia/Shanghai',)utc_now=datetime.utcnow().replace(tzinfo=timezone.utc)beijing_now=utc_now.astimezone(SHA_TZ)#定义headersheaders={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/70.0.3538.110Safari/537.36',}#定义baidu_headers,cookie需要BAIDUIDBDUSS,STOKENbaidu_headers={"user-agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/""97.0.4692.99Safari/537.36Edg/97.0.1072.69","Referer":"https://pan.baidu.com/disk/home?from=newversion&stayAtHome=true","cookie":baidu_cookie,'Connection':'close'}#获取bing每日一图defget_bing_pic():url='https://cn.bing.com/'r=requests.get(url,headers=headers)soup=BeautifulSoup(r.text,'html.parser')#寻找link标签,preloadBg图片的urllink=soup.find('link',id='preloadBg')#获取图片urlimg_url=link['href']#分割url通过&分割img_url=img_url.split('&')[0]#判断url是否有'1920x1080'如果有则替换为'UHD',UHD为4K图片标识if'1920x1080'inimg_url:img_url=img_url.replace('1920x1080','UHD')img_url="https://s.cn.bing.net"+img_url#获取图片名称img_name=img_url.split('=')[1]#返回图片urlreturnimg_url,img_name#判断网盘是否登录defis_login():url='https://pan.baidu.com/disk/home?from=newversion&stayAtHome=true#/all?path=%2F&vmode=list'r=requests.get(url,headers=baidu_headers)#通过网页源码判断是否登录if'百度网盘-全部文件'inr.text:print('网盘登录成功')returnTrueelse:print("更换COOKIE")returnFalse#获取logid,bdstokendefget_logid_bdstoken():#获取bdstoken的urlget_bdstoken_url="http://pan.baidu.com/api/gettemplatevariable?clienttype=0&app_id=250528&web=1&fields=[%22bdstoken%22]"bdstoken=requests.get(get_bdstoken_url,headers=baidu_headers).json().get('result').get('bdstoken')#获取logidlogid=str(base64.b64encode(requests.get(get_bdstoken_url,headers=baidu_headers).request.headers.get('cookie').split(';')[0].strip("BAIDUID=").encode('utf-8'))).replace("b'","").replace("'","")returnlogid,bdstoken#验证网盘目标目录文件夹是否存在defis_dir_exist(logid,bdstoken):url=f"https://pan.baidu.com/api/list?clienttype=0&app_id=250528&web=1&dp-logid={logid}&order=time&desc=1&dir=/图片&num=100&page=1"url1=f"https://pan.baidu.com/api/create?a=commit&channel=chunlei&web=1&app_id=250528&bdstoken={bdstoken}&logid={logid}&clienttype=0"data={"path":"/图片/"+str(beijing_now.date()),"isdir":1,'block_list':[]}r=requests.get(url,headers=baidu_headers)foriinr.json().get('list'):ifstr(beijing_now.date())ini.get('server_filename'):returnTrueelse:requests.post(url1,headers=baidu_headers,data=data)returnis_dir_exist(logid,bdstoken)defis_file_exist(logid,img_name):url=f"https://pan.baidu.com/api/list?clienttype=0&app_id=250528&web=1&dp-logid={logid}&order=time&desc=1&dir=/图片/{str(beijing_now.date())}&num=100&page=1"#发送请求r=requests.get(url=url,headers=baidu_headers)#判断是否下载成功iflen(r.json().get("list"))==0:print("今日未下载")returnTrueforiinr.json().get('list'):ifimg_nameini.get('path'):print('今天图片下载完成')returnFalse#网盘离线下载,参数为logid,bdstoken,图片urldefdownload_offline(logid,bdstoken,img_url):#拼接网盘离线下载urldownload_offline_url=f"https://pan.baidu.com/rest/2.0/services/cloud_dl?channel=chunlei&web=1&app_id=250528&bdstoken={bdstoken}&logid={logid}&clienttype=0"#定义datadata={"method":"add_task","app_id":"250528","source_url":img_url,"save_path":f"/图片/{str(beijing_now.date())}"#网盘目录}#发送请求r=requests.post(download_offline_url,headers=baidu_headers,data=data)if"Invalidsourceurl"inr.json():print("重新下载下载地址无效,请核对")download_offline(logid,bdstoken,img_url)if'vcode'inr.json():print("需要验证码调用")download_offline_captcha(logid,bdstoken,img_url,r)#需要验证码的离线下载defdownload_offline_captcha(logid,bdstoken,img_url,request):#拼接网盘离线下载urldownload_offline_url=f"https://pan.baidu.com/rest/2.0/services/cloud_dl?channel=chunlei&web=1&app_id=250528&bdstoken={bdstoken}&logid={logid}&clienttype=0"#通过ddddocr库识别验证码ocr=ddddocr.DdddOcr()#验证码图片captcha_img=requests.get(request.json()['img'],headers=baidu_headers).content#验证码ocrcaptcha=ocr.classification(captcha_img)print(captcha)#定义datadata={"method":"add_task","app_id":"250528","source_url":img_url,"save_path":f"/图片/{str(beijing_now.date())}",#网盘目录"input":captcha,"vcode":request.json()['vcode']}#发送请求r=requests.post(download_offline_url,headers=baidu_headers,data=data)print(r.json())if'vcode'inr.json():time.sleep(3)print('重新验证')download_offline_captcha(logid,bdstoken,img_url,request)#判断是否下载成功defis_download_success(logid,img_name):time.sleep(2)#拼接判断是否下载成功的urlurl=f"https://pan.baidu.com/api/list?clienttype=0&app_id=250528&web=1&dp-logid={logid}&order=time&desc=1&dir=/图片/{str(beijing_now.date())}&num=100&page=1"#发送请求r=requests.get(url,headers=baidu_headers)#判断是否下载成功iflen(r.json().get("list"))==0:print("未下载完成")time.sleep(2)returnis_download_success(logid,img_name)foriiinr.json().get('list'):ifimg_nameinii.get('path'):print('今天图片下载完成')returnFalseforiinr.json().get('list'):if"th.jpg"ini.get('path'):print('下载完成')returnTrueelse:print(i)print("有其他图片,继续下载")returnis_download_success(logid,img_name)defrechristen(logid,bdstoken,img_name):#拼接改名urlname_url=f"https://pan.baidu.com/api/filemanager?opera=rename&async=2&onnest=fail&channel=chunlei&web=1&app_id=250528&bdstoken={bdstoken}&logid={logid}&clienttype=0"#定义datadata={"filelist":'[{"path":"'+f"/图片/{str(beijing_now.date())}"+'/th.jpg","newname":"'+img_name+'"}]'}#发送请求requests.post(name_url,data,headers=baidu_headers)print("改名完成")if__name__=='__main__':img_url,img_name=get_bing_pic()ifis_login():logid,bdstoken=get_logid_bdstoken()ifis_dir_exist(logid,bdstoken)andis_file_exist(logid,img_name):download_offline(logid,bdstoken,img_url)ifis_download_success(logid,img_name):rechristen(logid,bdstoken,img_name)