爬取鬥圖網圖片

因爲初學python,就寫了簡單爬蟲,爬取了一些表情包,後期還會利用scrapy框架爬取小說等其他東西

廢話不多說,直接上代碼

#導入模塊
import  requests
from bs4 import BeautifulSoup
#定義路徑
path=r'D:\鬥圖2'
#給圖片取名字,從1開始
count=1
#爬取10頁表情包
for i in range(1,11):
    response=requests.get(r'http://www.bbsnet.com/page/'+str(i))
    response.encoding='utf-8'
    soup=BeautifulSoup(response.text,'html.parser')
    # imgs=soup.find_all('img')
    # print(imgs)
    imgs=soup.select('div > div > ul > li > div > a > img')
    for img in imgs:
        if img['src'][-3:]=='gif':
            data=requests.get(img['src'])
        # print(data)
            #路徑拼接
            file=open(path+'\\'+str(count)+img['src'][-4:],'wb')
            file.write(data.content)
            file.close()
            count+=1

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