[Python] 纯文本查看 复制代码
import random
import requests
from lxml import etree
from fake_useragent import UserAgent
import win32api
import win32gui
import win32con
import os
import re
import urllib
def set_wallpaper(bmp_path):
# 打开指定注册表路径
reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
# 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "2")
# 最后的参数:1表示平铺,拉伸居中等都是0
win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")
# 刷新桌面
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,bmp_path, win32con.SPIF_SENDWININICHANGE)
class bizhi_360():
def __init__(self):
bizhi_360 = ['http://bizhi360.com/meinv/list_{}.html'.format(i) for i in range(48)]
bizhi_360_one = random.choice(bizhi_360)
wang_zhan = [bizhi_360_one]
self.a = random.choice(wang_zhan)
def huoqu(self):
# 模拟访问者
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=self.a, headers=headers)
data.encoding= "utf-8"
data= data.text
s = etree.HTML(data)
meinv_liebiao = s.xpath('//*[@id="main"]/div/div[1]/ul/li/a//@href')
if len(meinv_liebiao) >1 :
meinv = random.choice(meinv_liebiao)
s = 'http://bizhi360.com'
url = s + meinv
self.xiazai(url)
else:
pass
def xiazai(self,url):
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=url, headers=headers)
data.encoding = "utf-8"
data =data.text
s = etree.HTML(data)
meinv_tupian = s.xpath('//*[@id="main"]/div/div[1]/div[2]/ul/li/a//@href')
if len(meinv_tupian) >1 :
meinv = random.choice(meinv_tupian)
s = 'http://bizhi360.com'
url1 = s + meinv
print(url1)
self.xiazai_tupian(url1)
else:
meinv_url = s.xpath('//*[@id="main"]/div/div[1]/div[2]/figure/a//@href')
try:
url = meinv_url[0]
response = requests.get(url,headers=headers)
# 获取的文本实际上是图片的二进制文本
img = response.content
# 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制文本
# 保存路径
path1 = os.getcwd()
path = path1 + '\\1.jpg'
with open(path, 'wb') as f:
f.write(img)
except Exception as ex:
print("出错")
def xiazai_tupian(self,url1):
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=url1, headers=headers)
data.encoding = "utf-8"
data = data.text
s = etree.HTML(data)
meinv_tupian = s.xpath('//*[@id="main"]/div[1]/div[1]/div[2]/figure/a//@href')
try:
url = meinv_tupian[0]
response = requests.get(url,headers=headers)
# 获取的文本实际上是图片的二进制文本
img = response.content
# 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制文本
# 保存路径
path1 = os.getcwd()
path = path1+ '\\1.jpg'
with open(path, 'wb') as f:
f.write(img)
except Exception as ex:
print("--------出错继续----")
class k4_bizhi():
def __init__(self):
num = random.randint(1,171)
bizhi_360 = ('http://pic.netbian.com/4kmeinv/index_{}.html'.format(num))
self.a = bizhi_360
def huoqu(self):
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=self.a, headers=headers)
data.encoding = "utf-8"
data = data.text
s = etree.HTML(data)
meinv_liebiao = s.xpath('//*[@id="main"]/div[3]/ul/li/a//@href')
if len(meinv_liebiao) >1 :
meinv = random.choice(meinv_liebiao)
s = 'http://pic.netbian.com'
url = s + meinv
self.xiazai(url)
else:
pass
def xiazai(self,url):
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=url, headers=headers)
data.encoding = "utf-8"
data = data.text
s = etree.HTML(data)
meinv_tupian = s.xpath('//*[@id="img"]/img//@src')
try:
s = 'http://pic.netbian.com'
url1 = s + meinv_tupian[0]
response = requests.get(url1, headers=headers)
# 获取的文本实际上是图片的二进制文本
img = response.content
# 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制文本
# 保存路径
path1 = os.getcwd()
path = path1 + '\\1.jpg'
with open(path, 'wb') as f:
f.write(img)
except Exception as ex:
print("--------出错继续----")
class k4_weimei():
def __init__(self):
num = random.randint(1, 6)
bizhi_360 = ('http://www.4kbizhi.com/meinv/index_{}.html'.format(num))
self.a = bizhi_360
def huoqu(self):
# 模拟访问者
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=self.a, headers=headers)
data.encoding= "utf-8"
data= data.text
s = etree.HTML(data)
meinv_liebiao = s.xpath('/html/body/div[2]/div[1]/ul/li/a//@href')
meinv_liebiao1 = s.xpath('/html/body/div[2]/div[2]/ul/li/a//href')
meinv_lie = meinv_liebiao +meinv_liebiao1
if len(meinv_lie) >1 :
meinv = random.choice(meinv_liebiao)
meinv= meinv.replace('.html', '')
s = 'http://www.4kbizhi.com'
url = s + meinv + '-1920x1080' +'.html'
self.xiazai(url)
else:
pass
def xiazai(self,url):
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=url, headers=headers)
data.encoding = "utf-8"
data =data.text
s = etree.HTML(data)
meinv_tupian = s.xpath('/html/body/div[2]/img//@src')[0]
b = meinv_tupian.replace('small', '')
c = b.split('/')
c.pop()
c = '/'.join(c)
b = b.split('/')[-1]
b = b[:11]
meinv_tupian_url = c + '/' + b + '.jpg'
try:
s = 'http://www.4kbizhi.com'
url1 = s + meinv_tupian_url
response = requests.get(url1,headers=headers)
# 获取的文本实际上是图片的二进制文本
img = response.content
# 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制文本
# 保存路径
path1 = os.getcwd()
path = path1 + '\\1.jpg'
with open(path, 'wb') as f:
f.write(img)
except Exception as ex:
print("出错")
class tupian_zhijia():
def __init__(self):
num = random.randint(1, 11)
bizhi_360 = ('https://www.tupianzj.com/bizhi/DNmeinv/list_77_{}.html'.format(num))
self.a = bizhi_360
def huoqu(self):
# 模拟访问者
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=self.a, headers=headers)
data.encoding= "utf-8"
data= data.text
s = etree.HTML(data)
meinv_liebiao = s.xpath('//*[@id="container"]/div/div/div[3]/div/ul/li/a//@href')
if len(meinv_liebiao) >1 :
meinv = random.choice(meinv_liebiao)
meinv= meinv.replace('.html', '')
s = 'https://www.tupianzj.com'
url = s + meinv + '_1920x1080' +'.html'
self.xiazai(url)
else:
pass
def xiazai(self,url):
ua = UserAgent().random
headers = {"User-Agent": ua}
data = requests.get(url=url, headers=headers)
data.encoding = "utf-8"
data = data.text
meinv_tupian = re.findall(r"if\(imgSize==\"1920x1080\"\) picurl='.*",data)
meinv_tupian = ''.join(meinv_tupian)
meinv_tupian = meinv_tupian.replace('\'', '')
meinv_tupian = meinv_tupian.replace(';', '')
meinv_tupian = meinv_tupian.split('=')[-1]
meinv_tupian1 = ''.join(meinv_tupian)
self.xiazai_tupian(meinv_tupian1)
def xiazai_tupian(self,url1):
try:
ua = UserAgent().random
url = str(url1)
headers = {"User-Agent": ua}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
# 获取的文本实际上是图片的二进制文本
img = response.read()
# response = requests.get(url, headers=headers)
# img = response.content
# 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制文本
# 保存路径
path1 = os.getcwd()
path = path1 + '\\1.jpg'
with open(path, 'wb') as f:
f.write(img)
except Exception as ex:
print("出错")
if __name__== '__main__':
liebiao = [1,2,3,4]
a= random.choice(liebiao)
if a ==1:
bizhi_360().huoqu()
elif a==2:
k4_bizhi().huoqu()
elif a ==3:
k4_weimei().huoqu()
elif a==4:
tupian_zhijia().huoqu()
path1 = os.getcwd()
path = path1 +"\\1.jpg" # 图片路径
try:
set_wallpaper(path)
except:
print('图片为下载!')