Python 网页爬虫 爬取网页图片demo

爬取 http://www.8she.com/31988.html 网页的图片

"""
Created on Thu May 17 20:21:11 2018

@author: Administrator
"""

import requests
from bs4 import BeautifulSoup
import os
res = requests.get('http://www.8she.com/31988.html')
res.edcoding = 'utf-8'
#print(res.text)
soup = BeautifulSoup(res.text,'html.parser')

imglists = soup.select('.aligncenter')

links = []

for imgsrc in imglists:
    links.append(imgsrc.get('src'))
    

path = 'E:\\PythonSpace\Img_spider\photo'
if not os.path.exists(path):
    os.makedirs(path)

i = 0
for link in links:
    pic = requests.get(link)
    img_path = path+'\\'+str(i)+'.jpg'
    if pic.status_code == 200:
        with open(img_path,'wb') as file:
            file.write(pic.content)
            i= i+1
    



猜你喜欢

转载自blog.csdn.net/qq_29796781/article/details/80356641