python利用selenium爬取图片信息
项目目标:通过运用python爬虫技术,模仿人为操作,克服反爬机制,爬取想要得到的信息
项目流程图
taob_view.py
from selenium.webdriver import Chrome #引入selenium中的Chrome
from selenium.webdriver.common.keys import Keys #引入键盘
import time #时间模块
import requests
browser=Chrome() #创建浏览器
browser.get("http://www.taobao.com")
#找到输入框,输入男装并且回车
browser.find_element_by_xpath('//*[@id="q"]').send_keys("男装",Keys.ENTER)
#让程序等待,用户手动登录
while browser.current_url.startswith("https://login.taobao.com"):
print("等着")
time.sleep(1)
n=1
#找到页面的所有item
while 1:
items=browser.find_element_by_class_name("m-itemlist").find_elements_by_class_name("item")
for item in items:
src_path=item.find_element_by_class_name("pic-box").find_element_by_tag_name("img").get_attribute("data-src")
src_path="http:"+src_path
#下载这张图片,保存在文件中
open(f"{n}.jpg",mode="wb").write(requests.get(src_path).content)
n+=1
browser.find_element_by_class_name("m-page").find_element_by_class_name("next").click()
time.sleep(2)
print("下一页了")