【selenium爬蟲】Yhen手把手帶你用selenium自動(dòng)化爬蟲爬取海賊王動(dòng)漫圖片

照著做了一篇,下載了10幾個(gè)圖片,然后,遇到如下錯(cuò)誤,求解.
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001B1904ACF08>: Failed to establish a new connection: [WinError 10060] 由于連接方在一段時(shí)間后沒(méi)有正確答復(fù)或連接的主機(jī)沒(méi)有反應(yīng),連接嘗試失敗。

文章來(lái)源:
原文鏈接:https://blog.csdn.net/Yhen1/article/details/105585188
webdriver下載地址:webdriver下載地址

from selenium import  webdriver #調(diào)用webdriver模塊
from selenium.webdriver.chrome.options import Options # 調(diào)用Options類
from pyquery import PyQuery as pq
import time
import requests
import os
import datetime

chrome_options = Options() # 實(shí)例化Option
chrome_options.add_argument('--headless') # 設(shè)置瀏覽器啟動(dòng)類型為靜默啟動(dòng)
driver = webdriver.Chrome(options = chrome_options) # 設(shè)置瀏覽器引擎為Chrome

#對(duì)首頁(yè)進(jìn)行請(qǐng)求
driver.get('https://image.baidu.com/search/index?ct=&z=&tn=baiduimage&ipn=r&word=%E5%A3%81%E7%BA%B8%20%E5%8D%A1%E9%80%9A%E5%8A%A8%E6%BC%AB%20%E6%B5%B7%E8%B4%BC%E7%8E%8B&pn=0&istype=2&ie=utf-8&oe=utf-8&cl=&lm=-1&st=-1&fr=&fmq=1587020770329_R&ic=&se=&sme=&width=1920&height=1080&face=0&hd=&latest=&copyright=')
# 執(zhí)行24次下滑到底部操作
for a in range(25):
     # 將滾動(dòng)條移動(dòng)到頁(yè)面的底部
     js = "var q=document.documentElement.scrollTop=1000000"
     driver.execute_script(js)
     # 設(shè)置一秒的延時(shí) 防止頁(yè)面數(shù)據(jù)沒(méi)加載出來(lái)
     time.sleep(1)
# 返回頁(yè)面源碼
response = driver.page_source

# 數(shù)據(jù)初始化
doc = pq(response)
# 通過(guò)類選擇器提取數(shù)據(jù)
x = doc("li.imgitem").items()
count = 0
# 遍歷數(shù)據(jù)
for main_img in x :
#    print(x)
     # 通過(guò)屬性“data-imgurl”取出圖片鏈接
    image_url = main_img.attr("data-objurl")
    print(image_url)
#對(duì)圖片鏈接發(fā)送請(qǐng)求,獲取圖片數(shù)據(jù)
    image =requests.get(image_url)

    date = datetime.datetime.now().strftime('%Y-%m-%d') + "海賊王" + "http://"
    # 判斷是否存在此文件夾
    folder = os.path.exists(date)
    # 如果不存在就新建該文件夾
    if not folder:
        os.makedirs(date)

    # 在海賊王圖片下載文件夾下保存為jpg文件,以“wb”的方式寫入 w是寫入 b是進(jìn)制轉(zhuǎn)換
    f = open(date + "{}.jpg".format(count), "wb")
     # 將獲取到的數(shù)據(jù)寫入 content是進(jìn)制轉(zhuǎn)換
    f.write(image.content)
     # 關(guān)閉文件寫入
    f.close()
     # 意思是 count = count + 1
    count+=1


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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