python(BeautifulSoup爬)下载必应美图

本文介绍如何利用Python的BeautifulSoup库抓取必应美图的无水印图片,作为壁纸使用。内容包括2019年1月23日、1月28日和3月10日的更新,逐步增加了国际版壁纸搜索等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到必应美图没有水印了,就想着可以拿来做壁纸了,(早想这么干了)python也是现学现用的,写得很烂,只求能用程度

用python的BeautifulSoup爬出来链接,再用urllib下载文件

import urllib.request
import urllib3
import os
from bs4 import BeautifulSoup

#下载进度显示
def Schedule(a,b,c):
    per = 100.0 * a * b / c
    if per > 100.0 :
    	print('success')
#获取网页流
URL='http://cn.bing.com'
http=urllib3.PoolManager()
bing=http.request('GET',URL)
#解析出文件地址
soup=BeautifulSoup(bing.data,"html.parser")
imgbg=soup.select('#bgImg')[0]
fileULR=URL+imgbg.get('src')
fileName=fileULR.split('/')[-1]
#开始下载文件
urllib.request.urlretrieve(fileULR,'../Pictures/Wallpaper/'+fileName,Schedule)
#保存url到日志文件
open('./url.txt','a',encoding='utf-8').write(fileULR+'\n')

2019.1.23更新(原标签节点已消失,更新其他标签节点)

import urllib.request
import urllib3
import os
from bs4 import BeautifulSoup

def Schedule(a,b,c):
    per = 100.0 * a * b / c
    if per > 100.0 :
    	print('success')

URL='http://cn.bing.com'
http=urllib3.PoolManager()
bing=http.request('GET',URL)
soup=BeautifulSoup(bing.data,"html.parser")
imgbg=soup.select('#bgLink')[0]
fileULR=URL+imgbg.get('href')
fileName=fileULR.split('/')[-1]
urllib.request.urlretrieve(fileULR,'C:/Users/Hyz/OneDrive/Wallpaper/'+fileName,Schedule)
open('./url.txt','a',encoding='utf-8').write(fileULR+'\n')

2019.1.28更新,多加国际版搜索壁纸

import urllib.request
import urllib3
import os
from bs4 import BeautifulSoup

#忽略证书验证
urllib3.disable_warnings()
def Schedule(a,b,c):
    per = 100.0 * a * b / c
    if per > 100.0 :
    	print('100% success')
    else:
    	sys.stdout.write(str(format(per,".1f"))+'%\r')#占行数字进度效果
    	sys.stdout.flush()

URL='http://cn.bing.com'
def bingImg(url):
	http=urllib3.PoolManager()
	bing=http.request('GET',url)
	soup=BeautifulSoup(bing.data,"html.parser")
	imgbg=soup.select('#bgLink')[0]
	fileULR=URL+imgbg.get('href')
	fileName=fileULR.split('/')[-1]
	urllib.request.urlretrieve(fileULR,'C:/Users/Hyz/OneDrive/Wallpaper/'+fileName,Schedule)
	open('./url.txt','a',encoding='utf-8').write(fileULR+'\n')
bingImg(URL)
bingImg('https://cn.bing.com/?FORM=BEHPTB&ensearch=1')

2019.3.10更新

import urllib3
import os
import sys
from urllib import request,parse
from bs4 import BeautifulSoup

#忽略证书验证
urllib3.disable_warnings()
def Schedule(a,b,c):
    per = 100.0 * a * b / c
    if per > 100.0 :
    	print('100% success')
    else:
    	sys.stdout.write(str(format(per,".1f"))+'%\r')#占行数字进度效果
    	sys.stdout.flush()

URL='http://cn.bing.com'
def bingImg(url):
	http=urllib3.PoolManager()
	bing=http.request('GET',url)
	soup=BeautifulSoup(bing.data,"html.parser")
	imgbg=soup.select('#bgLink')[0]
	fileULR=URL+imgbg.get('href')
    #解析URL参数得出文件名
	fileName="".join(parse.parse_qs(parse.urlparse(fileULR).query)['id'])
	request.urlretrieve(fileULR,'C:/Users/Hyz/OneDrive/Wallpaper/'+fileName,Schedule)
	open('./url.txt','a',encoding='utf-8').write(fileULR+'\n')
bingImg(URL)
#bingImg('https://cn.bing.com/?FORM=BEHPTB&ensearch=1')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值