爬虫代码,网页需要点击翻页的selenium

网页是需要翻页的,利用selenium库实现

代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

tageturl='www.'


browser = webdriver.Chrome()
browser.get(tageturl)
time.sleep(5)
#print(browser.page_source)
page=browser.find_elements_by_class_name("page_zoom_panel")#要爬取的那一class
print(page[0].get_attribute("src"))#提取该类下的属性sec值,即为爬取链接
fanye=browser.find_element_by_class_name("icon-epub-right")#翻页的按钮对于的网页源代码
fanye.click()#翻页鼠标点击操作
browser.close()#关闭浏览器页面

图片爬取下载代码:

import requests
import os
url='http:.....8bf730c659214879e9f37c5f2d.jpg'
root = 'D://bookpic//'#定义保存本地的根目录
path=root+url.split('/')[-1]#将文件名称与网络名相一致
try:
	if not os.path.exists(root):#判断该根目录是否存在,若不存在则创建
		os.mkdir(root)
	if not os.path.exists(path):#判断该文件是否存在,若不存在则获取
		r=requests.get(url)
		with open(path,'wb') as f:
			f.write(r.content)
			f.close
			print('图片保存成功')
	else:
		print('图片已经存在')
except:
	print('爬取失败')

#	

 

你可能感兴趣的:(爬虫)