爬虫实战11:爬取aiss图片并保存

import requests
import bs4
import urllib.request

url = "http://www.ligui.org/aiss/"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'}


def get_page():
    response = requests.get(url, headers=headers)
    html = response.text
    return html

def page_parse(html):
    soup = bs4.BeautifulSoup(html, 'lxml')
    imgs = soup.find('ul', class_='img')
    imgs_list = imgs.find_all('li')
    i = 1
    for img_list in imgs_list:
        img = img_list.find('a').find('img')['src']
        urllib.request.urlretrieve(img,'G:\python\爬虫实战\images\\' + str(i) + '.png')
        i += 1


def main():
    html = get_page()
    page_parse(html)

if __name__ == '__main__':
    main()

你可能感兴趣的:(爬虫实战,爬虫实战,爬虫实战)