python抓取豆瓣圖片并自動保存示例學習
環(huán)境Python 2.7.6,BS4,在powershell或命令行均可運行。請確保安裝了BS模塊
# -*- coding:utf8 -*-
# 2013.12.36 19:41 wnlo-c209
# 抓取dbmei.com的圖片。
from bs4 import BeautifulSoup
import os, sys, urllib2
# 創(chuàng)建文件夾,昨天剛學會
path = os.getcwd() # 獲取此腳本所在目錄
new_path = os.path.join(path,u'豆瓣妹子')
if not os.path.isdir(new_path):
os.mkdir(new_path)
def page_loop(page=0):
url = 'http://www.dbmeizi.com/?p=%s' % page
content = urllib2.urlopen(url)
soup = BeautifulSoup(content)
my_girl = soup.find_all('img')
# 加入結束檢測,寫的不好....
if my_girl ==[]:
print u'已經(jīng)全部抓取完畢'
sys.exit(0)
print u'開始抓取'
for girl in my_girl:
link = girl.get('src')
flink = 'http://www.dbmeizi.com/' + link
print flink
content2 = urllib2.urlopen(flink).read()
with open(u'豆瓣妹子'+'/'+flink[-11:],'wb') as code: #在OSC上現(xiàn)學的
code.write(content2)
page = int(page) + 1
print u'開始抓取下一頁'
print 'the %s page' % page
page_loop(page)
page_loop().
相關文章
為什么在函數(shù)中運行的?Python?代碼速度更快?
對于Python解釋器來說,讀取和寫入局部變量比全局變量更容易和更快,因為它們的作用域范圍較小2023-09-09
Python日期與時間模塊(datetime+time+Calendar+dateuil?)相關使用講解
這篇文章主要介紹了Python日期與時間模塊(datetime+time+Calendar+dateuil?)相關使用講解,文章圍繞主題展開詳細的內容戒殺,具有一定的參考價值,需要的朋友可以參考一下2022-09-09
python3新特性函數(shù)注釋Function Annotations用法分析
這篇文章主要介紹了python3新特性函數(shù)注釋Function Annotations用法,結合實例形式分析了Python3函數(shù)注釋的定義方法與使用技巧,需要的朋友可以參考下2016-07-07
matplotlib jupyter notebook 圖像可視化 plt show操作
這篇文章主要介紹了matplotlib jupyter notebook 圖像可視化 plt show操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

