§01 代码清单
class get_random_wallhaven(object):def __init__(self, url = 'https://wallhaven.cc/random'):self.init_url = urldef getHTMLText(self, url):import urllib3headers = {'User -Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'}#proxy ={'http': '58.249.55.222:9797'} #proxies=proxy 代理try:urllib3.disable_warnings()r = requests.get(url, headers=headers, verify=False)r.raise_for_status() r.encoding = r.apparent_encodingreturn r.textexcept:return "产生异常"def getImgPageUrl(self, text):url = ''soup = BeautifulSoup(text)img_a = soup.select('a[class="preview"]')for a in list(img_a)[:1]:try:url = a.attrs['href']except:passreturn urldef getImgUrl(self, text):url = ''soup = BeautifulSoup(text)img = soup.select('img[id="wallpaper"]')for i in list(img)[:1]:try:url = i.attrs['src']except:passreturn urldef ImgUrl(self):text = self.getHTMLText(self.init_url)url = self.getImgPageUrl(text)text = self.getHTMLText(url)url = self.getImgUrl(text)return url
§02 调用方法
一、默认方式
wh = get_random_wallhaven()
txtstr = wh.ImgUrl()
二、修改搜索条件
1、打开网页
https://wallhaven.cc/random
▲ 图1 修改搜索条件说明
2、获取新的url
▲ 图2 新的url
3、使用新的url
url = 'https://wallhaven.cc/search?categories=110&purity=100&ratios=16x9%2C21x9&sorting=random&order=desc'
wh = get_random_wallhaven(url)
txtstr = wh.ImgUrl()