龙空技术网

「python学习」使用python爬取壁纸

IT动力 3803

前言:

此刻朋友们对“python主题壁纸”可能比较重视,姐妹们都想要学习一些“python主题壁纸”的相关资讯。那么小编在网摘上网罗了一些对于“python主题壁纸””的相关文章,希望朋友们能喜欢,看官们快快来了解一下吧!

代码实现

话不多说,直接上代码,拿走不谢!

手机壁纸爬取(国外)

import requestsimport jsonimport timedef test():    list = []    for i in range(1, 10):        if i == 1:            url = ";        else:            url = f"{i}&per_page=20"        list.append(url)    # 模拟浏览器    headers = {        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"}    i = 1    for uri in list:        # 请求数据        request = requests.get(uri, headers=headers, verify=False)        # 设置charset格式        resp = request.content.decode('utf-8')        json_res = json.loads(resp)        photos = json_res["photos"]        j = 1        for photo in photos:            urls = photo["urls"]            img_url = urls["regular"]            img_content = requests.get(img_url)            # 获取图片内容            img_byte = img_content.content            path = f"D:/project_workspace/crawling_data/unsplash/{i}_{j}.jpg"            with open(path, "wb") as f:                f.write(img_byte)                time.sleep(0.1)            print(f"第{i}页第{j}张图片你下载成功")            j = j + 1        i = i + 1    return "success"if __name__ == '__main__':    test()

图片下载如下

电脑壁纸爬取(国内)

    base_url = ";    list = []    for i in range(1, 100):        if i == 1:            url = ";        else:            url = f"{i}.html"        list.append(url)    # 模拟浏览器    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"}    i = 1    for uri in list:        # 请求数据        request = requests.get(uri, headers=headers, verify=False)        # 设置charset格式        resp = request.content.decode('gbk')        # 通过BeautifulSoup进行数据解析        soup = BeautifulSoup(resp, "lxml")        divs = soup.find_all("div", class_="slist")        j = 1        for imgs in divs:            img = imgs.find_all("img")            for im in img:                img_src = im["src"]                img_uri = base_url + img_src                img_content = requests.get(img_uri)                # 获取图片内容                img_byte = img_content.content                path = f"D:/project_workspace/crawling_data/{i}_{j}.jpg"                with open(path, "wb") as f:                    f.write(img_byte)                    time.sleep(0.1)                print(f"第{i}页第{j}张图片你下载成功")                j = j + 1        i = i + 1

代码很简单,由于国外网站,网速较慢。这里附上壁纸。

手机壁纸分享

壁纸太多,随机附上数张,可自己使用程序下载。

电脑壁纸分享

标签: #python主题壁纸