一、python爬蟲程序入門(圖片下載)

http://www.duitang.com/search/?kw=%E6%96%87%E8%B1%AA%E9%87%8E%E7%8A%AC&type=feed#!s-p1

這個url下面的圖片具備這樣的規律:

<a target="_blank" class="a" href="/blog/?id=728506209">
<img data-rootid="728506209" alt="文豪野犬" data-iid="" src="https://b-ssl.duitang.com/uploads/item/201703/22/20170322201614_PwXnz.thumb.224_0.jpeg" height="329">
<u style="margin-top:-329px;height:327px;" class=""></u>
</a>

——見紅色字體


#!/usr/bin/python
#-*- coding: utf-8 -*-
#encoding=utf-8
import urllib
from urllib import request
import os
from bs4 import BeautifulSoup

def getAllImageLink():

    html = request.urlopen('http://www.duitang.com/search/?kw=%E6%96%87%E8%B1%AA%E9%87%8E%E7%8A%AC&type=feed#!s-p1').read()
    #print(len(html))
    soup = BeautifulSoup(html, "html.parser")
    #print(soup.prettify())
    li_request = soup.find_all('a', class_='a')
    #print (li_request)
    count = 0
    for myimg in li_request:
        link = myimg.find('img').get('src')
        pic_name = 'F:/image/python/' + str(count) + '.jpg'
        urllib.request.urlretrieve(link, pic_name)
        count += 1

if __name__ == '__main__':
    getAllImageLink()


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