python制作壁纸获取器exe,壁纸采集
【为想学习爬虫的小白朋友分享的入门级代码,一键获取大量壁纸】
sorry:
很久没更新了,因为参加了实训就紧接着去学车了(每天都是车少人多,最多练三次),最近学习了很多,有数据挖掘和数据分析实战,以及大数据比赛 的题解,接下来慢慢分享给大家,不多说,又该去练一下午车了
效果:
1思路:
1、每个图片在一个li标签里,我们拿取图片详情页的url
2、图片详情页里h1为name,img的src属性为下载地址后缀
3、构造下载地址:“https://pic.netbian.com/”+地址后缀
4、最后进行请求下载
5、使用pyinstaller库进行打包为exe
2、实现过程:
获取第一页的图片所有url:
主要使用xpath语句进行提取:
xpath_1 = '//*[@id="main"]/div[3]/ul//li/a/@href'
提取href属性,是网址后缀
第一部分代码:返回第一页的所有图片详情页的网址后缀,列表形式
import requests
from lxml import etree
import os
import time
def get_1(i):
url1 = 'https://pic.netbian.com/new/index_{}.html'.format(i)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
response=requests.get(url1,headers=header)
s = response.content
s.decode('gbk')
xpath_1 = '//*[@id="main"]/div[3]/ul//li/a/@href'
html = etree.HTML(s)
imginofurl = html.xpath(xpath_1)
print((imginofurl))
return imginofurl
第二页,图片详情页,获取图片名称和下载地址:
xpath提取name:
name_xpath = '//*[@id="main"]/div[2]/div[1]/div[1]/h1/text()'
/text()代表提取文字段
xpath 提取下载地址:
img_xpath = '//*[@id="img"]/img/@src'
imageurl = 'https://pic.netbian.com/'+html1.xpath(img_xpath)[0]
进行拼接得到下载地址
第二部分代码:
def get_2(urls,newimg_path):
imgs =[]
for url in urls:
imginof = 'https://pic.netbian.com/'+url
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
response = requests.get(imginof, headers=header)
s = response.content
s.decode('gbk')
img_xpath = '//*[@id="img"]/img/@src'
html1 = etree.HTML(s)
imageurl = 'https://pic.netbian.com/'+html1.xpath(img_xpath)[0]
print(imageurl)
name_xpath = '//*[@id="main"]/div[2]/div[1]/div[1]/h1/text()'
imgname = html1.xpath(name_xpath)[0]
print(imgname)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
content = requests.get(url=imageurl, headers=header).content
with open(newimg_path+'/'+'{}.jpg'.format(imgname), 'ab+') as f:
f.write(content)
print("壁纸{}保存完成".format(imgname))
完整代码:
import requests
from lxml import etree
import os
import time
def get_1(i):
url1 = 'https://pic.netbian.com/new/index_{}.html'.format(i)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
response=requests.get(url1,headers=header)
s = response.content
s.decode('gbk')
xpath_1 = '//*[@id="main"]/div[3]/ul//li/a/@href'
html = etree.HTML(s)
imginofurl = html.xpath(xpath_1)
print((imginofurl))
return imginofurl
def get_2(urls,newimg_path):
imgs =[]
for url in urls:
imginof = 'https://pic.netbian.com/'+url
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
response = requests.get(imginof, headers=header)
s = response.content
s.decode('gbk')
img_xpath = '//*[@id="img"]/img/@src'
html1 = etree.HTML(s)
imageurl = 'https://pic.netbian.com/'+html1.xpath(img_xpath)[0]
print(imageurl)
name_xpath = '//*[@id="main"]/div[2]/div[1]/div[1]/h1/text()'
imgname = html1.xpath(name_xpath)[0]
print(imgname)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
content = requests.get(url=imageurl, headers=header).content
with open(newimg_path+'/'+'{}.jpg'.format(imgname), 'ab+') as f:
f.write(content)
print("壁纸{}保存完成".format(imgname))
time.sleep(5)
if not os.path.exists('../壁纸/'):
os.makedirs('./壁纸/')
newimg_path = './壁纸/'
for i in range(2,30):
urls = get_1(i)
get_2(urls,newimg_path)
time.sleep(5)
进行打包为exe:
找到代码所在文件夹路径,制作一个自己喜欢的ico图标,进行命令行输入打包:
pyinstaller -F -w -i tubiao.ico biantu.py
如果没有安装pyinstaller :
‘pip install pyinstaller -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com --upgrade
进行安装库
最后可以把dist文件夹中的exe文件拿出来单独运行:
exe下载链接:
链接:https://pan.baidu.com/s/1GwlNNsgjooGyfe4mUuxBhw 提取码:8848
|