因为百度图片的图片数据是存储在js里面的json中,直接使用BeautifulSoup的话无法获取json中的数据;而且下滑的过程中可以获取更多的图片,所以我就使用了selenium加载了页面,再提取图片。
怎么使用selenium参照我的 https://blog.csdn.net/yxw908186797/article/details/89217311 。
还有一种使用正则表达式直接提前js中的json中的数据,我也一并放到最后!
# coding=utf-8
import time
import requests
import threading
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
DefaultPath = "D:\\pictures\\" #默认储存位置
NeedSave = True #是否需要储存
def getPicture(num):
key = input("Please input you want search:")
StartSoure = "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&fm=index&pos=history&word="+key
browser = webdriver.Chrome('E:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')
browser.maximize_window()
browser.get(StartSoure)
time.sleep(1)
# 将屏幕上滑num次
for i in range(1,num):
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(1)
soup = BeautifulSoup