Python的一只虫子,爬取风景图

实例为爬取天堂图片网旧版,自然风光图片,共有576页,每页20张,可设置参数爬取!

import requests
from bs4 import BeautifulSoup
i =2
#i<=10,循环2-10,共9次,10可以改成存在页数的任意值,越大下载越多,此页面存在576页
while i<=10:
    url = "https://www.ivsky.com/Photo/1/1_List_{}.html".format(i)
    response = requests.get(url)
    response.encoding = "gb2312"
    main_page = BeautifulSoup(response.text,"html.parser")
    list1 = main_page.find("div",attrs={"class":"list_pic"}).find("ul").find_all("a")
    for a in list1:
        href1 = a.get("href")
        href = "https://www.ivsky.com/" + href1
        child_url = requests.get(href)
        child_url.encoding =  "gb2312"
        child_page = BeautifulSoup(child_url.text,"html.parser")
        img = child_page.find("td",attrs={"class":"picalign"}).find("img")
        #获取图片名称
        title = img.get("alt")
        p = open("{}.jpg".format(title),mode="wb")
        p.write(requests.get(img.get("src")).content)
        p.close()
        print(title)
    i+=1

初学者,欢迎并感谢各位大佬指点!

你可能感兴趣的:(python)