爬虫下载壁纸,并设置壁纸自动切换

贴代码(初版,没有排版,更改,封装):

1.爬虫部分

一开始找到百度壁纸,个人比较喜欢雪景,所以想用爬虫批量下载,结果发现百度壁纸是动态的,就用request结果还是不行,所以最后又不得不用phantomjs来获取网页远吗,后来获取源码以后,解析出来了图片的url地址,然而mdzz用urlretrieve下载,结果百度给403foribidden了,后来一直在找办法,什么访问带头部,GG,什么发放稳获取response然后写入文件,GG,最后想到了unbuntu系统里面的wget来下载于是写入文件然后wget-i文件名进行批量下载,可以下,但是wget访问频率太快,在下载十多张图片之后就会被百度封了,于是查看wget命令,发现-w(-wait)课进行休眠,于是准备合理化访问。

2.图片背景自动更换设置(win10)

个性化-背景-背景-幻灯片放映,选择图片存储的文件夹,done

import urllib
'''
from urllib import request
from bs4 import BeautifulSoup as bs

url = "http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%D1%A9%BE%B0%D7%C0%C3%E6%B1%DA%D6%BD&fr=ala&ala=1&pos=0&alatpl=wallpaper&oriquery=%E9%9B%AA%E6%99%AF%E6%A1%8C%E9%9D%A2%E5%A3%81%E7%BA%B8"
response = request.urlopen(url).read()
content = str(response,encoding = "utf-8")
bs_obj = bs(content,"html.parser")
print(bs_obj)

'''
#urlopen是最简单的但tmd也是最垃圾的over
#下面有请我们最高级的phatomjs
from selenium import webdriver

driver = webdriver.PhantomJS()
driver.set_window_size(25600,14400)
driver.get("http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%D1%A9%BE%B0%B1%DA%D6%BD&fr=ala&ala=1&pos=0&alatpl=wallpaper&oriquery=%E9%9B%AA%E6%99%AF%E5%A3%81%E7%BA%B8s")
page_source = driver.page_source
#print(page_source)
#print(page_source)
#有请伟大的re
#伟大的re失败了。。。这尼玛代码还会自动消失出现,!绝了
'''
import re
pattern = re.compile(r'src="http://.*?.jpg"')
img_src_list = pattern.findall(page_source,re.I)
url_pattern = re.compile(r'http://.*?.jpg')
img_url_list = []
for i in img_src_list:
    img_url_list.append(url_pattern.find(i,re.I))

for i in img_url_list:
    print(i)
'''
#有请老伙伴bs4
#init download path
download_path = r"C:\Users\Mr.Guo\Pictures\BDpictures"
from bs4 import BeautifulSoup
import requests
from urllib import request
bs_obj = BeautifulSoup(page_source,"html.parser")
img_url_list = bs_obj.findAll("img",{"class":"main_img img-hover"})
final_url_list = []
for i in img_url_list:
    final_url_list.append(i.attrs["src"])
#print(final_url_list)
f = open(download_path+"\test.txt",'a')
for i in range(len(final_url_list)):
    print(final_url_list[i])
    try:
        '''
        opener=request.build_opener()
        opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36')]
        request.install_opener(opener)
        #request.urlretrieve(url=url,filename=‘%s/%s.txt‘%(savedir,get_domain_url(url=url)))
        request.urlretrieve(final_url_list[i],download_path+"\%s.jpg"%i)
        '''
        '''
        r = requests.get(final_url_list[i])
        i_download_path = download_path + "\%s.jpg"%i
        with open(i_download_path, "wb") as code:
             code.write(r.content)
        '''
        f.write(final_url_list[i]+'\n')
    except Exception as e:
        print(e)
        pass   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了设置IP自动循环切换,你可以使用代理池和代理IP来实现。代理池是一个代理IP的池子,可以从中随机取出一个代理IP来使用。代理IP是一种中间代理服务器,它可以隐藏你的真实IP地址,并代替你发送请求。 下面是一个简单的Python代码示例,演示如何使用代理池和代理IP来实现IP自动循环切换: ```python import requests from itertools import cycle proxies = { 'http': 'http://127.0.0.1:8000', 'https': 'http://127.0.0.1:8000' } proxy_pool = cycle([ 'http://1.2.3.4:8080', 'https://5.6.7.8:8080', 'http://9.10.11.12:8080' ]) for i in range(10): # 从代理池中取出一个代理IP proxy = next(proxy_pool) print("Using proxy", proxy) # 使用代理IP发送请求 try: response = requests.get('http://example.com', proxies={'http': proxy, 'https': proxy}) print(response.text) except requests.exceptions.RequestException as e: print(e) ``` 在上面的代码中,我们首先定义了一个代理池,其中包含了多个代理IP。然后我们使用`cycle`函数来创建一个循环器,它可以从代理池中无限循环取出代理IP。最后我们使用`next`函数来从循环器中取出下一个代理IP,并使用`requests`库发送请求时指定这个代理IP。这样就可以实现IP自动循环切换了。 需要注意的是,代理池中的代理IP需要不定期地更新,否则可能会出现无效的代理IP。同时,使用代理IP也会降低请求速度,需要根据具体情况来选择合适的代理IP数量和更新频率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值