使用selenium实现站长素材图片采集
Posted wangtaobiu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用selenium实现站长素材图片采集相关的知识,希望对你有一定的参考价值。
from selenium import webdriver
import requests,os
from lxml import etree
from selenium.webdriver.chrome.options import Options
from urllib import request
chrome_options = Options()
chrome_options.add_argument(‘--headless‘)
chrome_options.add_argument(‘--disable-gpu‘)
pro = webdriver.Chrome(executable_path=‘./chromedriver.exe‘,options=chrome_options)
url = "http://sc.chinaz.com/tupian/haiyangshengwutupian.html"
pro.get(url)
js = ‘window.scrollTo(0,document.body.scrollHeight)‘
pro.execute_script(js)
page_text = pro.page_source
tree = etree.HTML(page_text)
url_img = tree.xpath(‘//div[@id="container"]/div[@class="box picblock col3 masonry-brick"]/div/a/img/@src‘) #获取图片url列表
names = tree.xpath(‘//div[@id="container"]/div[@class="box picblock col3 masonry-brick"]/div/a/@alt‘) #图片名称列表
if not os.path.exists(‘./img‘): #生成文件夹
os.mkdir(‘./img‘)
for index,url in enumerate(url_img):
img_path = ‘./img/‘ + names[index]+‘.jpg‘ #提取图片名称
request.urlretrieve(url,img_path)
以上是关于使用selenium实现站长素材图片采集的主要内容,如果未能解决你的问题,请参考以下文章
爬虫之 图片懒加载, selenium , phantomJs, 谷歌无头浏览器
Python网络爬虫之图片懒加载技术selenium和PhantomJS
08.Python网络爬虫之图片懒加载技术selenium和PhantomJS