当前位置:网站首页>Selenium crawls Baidu pictures
Selenium crawls Baidu pictures
2022-07-05 13:48:00 【Weichi Begonia】
Selenium Crawling Baidu pictures
# coding=utf-8
""" obtain 10 Baidu pictures """
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time, requests
def download_img(kw):
# Open the browser
browser = webdriver.Chrome()
time.sleep(3)
# The other party visits the corresponding web page
url = r'https://image.baidu.com/'
browser.get(url)
time.sleep(3)
# Enter the corresponding... In the input box key words
keyword = browser.find_element_by_id('kw')
keyword.send_keys(kw) # Enter key
keyword.send_keys(Keys.ENTER) # enter
time.sleep(3)
# Set the picture size
size = browser.find_element_by_id('sizeFilter')
big_size = browser.find_element_by_xpath('/html/body/div[1]/div[4]/div[2]/div/div[2]/div/div[1]')
ActionChains(browser).click(size).move_to_element(big_size).click().perform()
# Click on the first picture
first_pic = browser.find_element_by_xpath('//*[@id="imgid"]/div/ul/li[1]/div/a/img')
ActionChains(browser).click(first_pic).perform() # call perform() When the method is used , The events in the queue will execute in turn .
time.sleep(5)
# Switch to new window in
browser.switch_to.window(browser.window_handles[1])
for i in range(20):
# Get photo
pic = browser.find_element_by_xpath('//*[@id="currentImg"]')
src = pic.get_attribute('src')
r = requests.get(src)
title = browser.find_element_by_class_name('pic-title') # Output picture title at the same time
print(i, ' ', title.text)
if r.status_code == 200:
# Save the picture to a file
file_name = r'D:\1. learning\python_web\pachong\img\{}.jpg'.format(i)
with open(file_name, 'wb') as f:
f.write(r.content) # Use of words r.text / Other formats use r.content
# Switch to the next picture
next_btn = browser.find_element_by_class_name('img-next')
next_btn.click()
time.sleep(5)
return
if __name__ == '__main__':
download_img(' Mickey Mouse ')
边栏推荐
- Zhubo Huangyu: these spot gold investment skills are not really bad
- 记录一下在深度学习-一些bug处理
- zabbix 监控
- What about data leakage? " Watson k'7 moves to eliminate security threats
- Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
- Kotlin collaboration uses coroutinecontext to implement the retry logic after a network request fails
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- Don't know these four caching modes, dare you say you understand caching?
- Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
- Record in-depth learning - some bug handling
猜你喜欢
Assembly language - Beginner's introduction
Godson 2nd generation burn PMON and reload system
What about data leakage? " Watson k'7 moves to eliminate security threats
Self built shooting range 2022
Solve the problem of invalid uni app configuration page and tabbar
French scholars: the explicability of counter attack under optimal transmission theory
TortoiseSVN使用情形、安装与使用
Jasypt configuration file encryption | quick start | actual combat
嵌入式软件架构设计-消息交互
STM32 reverse entry
随机推荐
web3.eth. Filter related
Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
Laravel框架运行报错:No application encryption key has been specified
Ueditor + PHP enables Alibaba cloud OSS upload
Idea set method annotation and class annotation
Ordering system based on wechat applet
Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
Attack and defense world web WP
What are the private addresses
内网穿透工具 netapp
Data Lake (VII): Iceberg concept and review what is a data Lake
Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error
Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
[South China University of technology] information sharing of postgraduate entrance examination and re examination
Win10 - lightweight gadget
Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
jasypt配置文件加密|快速入门|实战
Usage, installation and use of TortoiseSVN
如何把大的‘tar‘存档文件分割成特定大小的多个文件