Python 爬蟲selenium爬煎蛋

初學(xué)爬蟲入坑爬煎蛋的教程,無奈煎蛋有反爬蟲機(jī)制獲取不到真實(shí)圖片地址,研究了兩天,自己寫了一個(gè),代碼很簡單,便于理解。

import time,requests
from selenium import webdriver
from bs4 import BeautifulSoup
star = time.time()
browser = webdriver.Chrome()
n = 1
total = 0
for num in range(48,0,-1):
    browser.get('http://jandan.net/ooxx/page-'+str(num)+'#comments')
    data = browser.page_source
    soup = BeautifulSoup(data,'lxml')
    download_links = []
    folder_path = 'C:\\Users\\Administrator\\Desktop\\JD\\'
# print('===========第' + str(num) + '頁===============')
    for pic_tag in soup.find_all('img'):
        pic_link = pic_tag.get('src')
        download_links.append(pic_link)
    for item in download_links:
        try:
            urllib.request.urlretrieve(item,folder_path + item[-10:])
                with open(folder_path + item[-10:],'wb',) as f:
                    f.write(requests.get(item).content)
            print('正在下載第{}圖片'.format(n))
            total += 1
        except:
            print('第{}張圖片下載出錯(cuò),已跳過'.format(n))
            n += 1
browser.close()
end = time.time()
print('總共用時(shí){}分'.format((end-star)/60))
print('成功下載{}張圖片,失敗{}張圖片'.format(total,n-total))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容