Python爬蟲案例Demo——某網站壁紙的爬取

這是當時第二天的案例,是一個著名的高清壁紙網站:

import requests
import re
url = "https://wallhaven.cc/"
headers = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}
response = requests.get(url,headers = headers)
response.encoding = response.apparent_encoding
html = response.text


# https://wallhaven.cc/w/j5k825
# <a href="https://wallhaven.cc/w/j5k825"><img src="https://th.wallhaven.cc/small/j5/j5k825.jpg" width="300px" alt="" /></a>
result = re.findall('<a href="(.*?)"><img src="(.*?)" width="(.*?)" alt="" /></a>',html)
for url in result:
    new_url = str(url).split(',')[1]
    urls = eval(new_url)
    print(urls)
    image_response = requests.get(urls, headers=headers)
    # image = image_response.replace("/'",'')
    filename = new_url.split('/')[-1].split("'")[0]
    with open(str(filename), mode="wb") as t:
        t.write(image_response.content)

截止到目前爲止,這些代碼是可以運行的,大家可以看下Pycharm中的運行結果:
在這裏插入圖片描述
爬取的照片結果是:
在這裏插入圖片描述
這是第二天的案例,大家可以加上一些換頁的操作等等!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章