from bs4 import BeautifulSoup import re import requests import os
url1 = '主URL' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 ' 'Safari/537.36 ' }
response_html = requests.get(url=url1, headers=headers, verify=False).text
for i in range(2, 70): os.makedirs('./img/', exist_ok=True)
soup = BeautifulSoup(response_html, 'lxml') img_url = soup.find_all('a', {"class": "featured-img-box"}) print("该页有链接{}个图集".format(len(img_url))) for iu in img_url: url_img = iu['href'] response_imgPage = requests.get(url=url_img, headers=headers).text imgPage_soup = BeautifulSoup(response_imgPage, 'lxml') img_link = imgPage_soup.find_all('img', {"title": "source: imgur.com"}) for il in img_link: link = il['src'] print('图片链接:', link) if link[0:9] == 'https://i': break r = requests.get(link, stream=True, verify=False) image_name = link.split('/')[-1] with open('./img/%s' % image_name, 'wb') as f: for chunk in r.iter_content(chunk_size=128): f.write(chunk) print('Save %s' % image_name) ul2 = '子URL' + str(i) response_html = requests.get(url=ul2, headers=headers).text
|