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
    



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