selenium爬取煎蛋网
selenium爬取煎蛋网
直接上代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ES
import requests
import urllib.request
import os
from lxml import etree
t = 0
class Custer(object):
driver_path = r"D:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
def __init__(self):
self.driver = webdriver.Chrome(executable_path=self.driver_path)
self.url = "http://jandan.net/ooxx"
def run(self):
self.driver.get(self.url)
while True:
all_source = self.driver.page_source
html = etree.HTML(all_source)
self.xqy(html)
WebDriverWait(self.driver,10).until(
ES.presence_of_element_located((By.XPATH,"//div[@class='cp-pagenavi']/a[last()]"))
)
try:
Btn = self.driver.find_element_by_xpath("//div[@class='cp-pagenavi']/a[last()]")
if "Older Comments" in Btn.get_attribute("title"):
Btn.click()
else:
break
except:
print("出现异常") def xqy(self,html):
all_content = html.xpath("//div[@class='row']//div")
all_author = all_content[0].xpath("//div[@class='author']/strong/text()") #作者列表 #*****************给自己的重点**********************
#给列表重复元素加工 如果不加工进入字典会少很多元素
for index,item in enumerate(all_author):
global t
if item in all_author[0:index]: #判断当前元素是否与之前元素重复 如果重复,则重命名
t=t+1
all_author[index] = item+str(t) #如多个重命名使作者加上字符1 依次类推
#*************************************************** WebDriverWait(self.driver, 10).until(
ES.presence_of_element_located((By.XPATH, "//div[@class='text']//img"))
)
all_img = all_content[1].xpath("//div[@class='text']//img//@src") #图片列表
#解决有个张图片没有http:协议
for index,item in enumerate(all_img):
if 'http:' not in item:
all_img[index] = 'http:'+item dic = dict(zip(all_author,all_img)) #多个列表生产字典
#遍历字典保存图片
for key in dic:
hz = os.path.splitext(dic[key])[1] #取出后缀名.jpg/.png
filename = key+hz #文件名(标题+后缀名)
urllib.request.urlretrieve(dic[key],'images/'+filename) def main():
rea = Custer()
rea.run() if __name__ == '__main__':
main()
爬取的图片

进阶
个人用了个多线程 但不知道是不是多线程爬取 感觉爬取速度快多了
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ES
import requests
import threading
import urllib.request
import os
from lxml import etree
t = 0
gCondition = threading.Condition()
class Custer(threading.Thread):
driver_path = r"D:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path)
url = "http://jandan.net/ooxx"
def run(self):
self.driver.get(self.url)
while True:
all_source = self.driver.page_source
html = etree.HTML(all_source)
self.xqy(html)
WebDriverWait(self.driver,10).until(
ES.presence_of_element_located((By.XPATH,"//div[@class='cp-pagenavi']/a[last()]"))
)
gCondition.acquire() #加上锁(如果不加锁那么多个线程可能同时请求一个或多个图片)
try:
Btn = self.driver.find_element_by_xpath("//div[@class='cp-pagenavi']/a[last()]")
if "Older Comments" in Btn.get_attribute("title"):
gCondition.release() #解锁
Btn.click()
else:
break except:
print("出现异常") def xqy(self,html):
all_content = html.xpath("//div[@class='row']//div")
all_author = all_content[0].xpath("//div[@class='author']/strong/text()") #作者列表 #*****************给自己的重点**********************
#给列表重复元素加工 如果不加工进入字典会少很多元素
for index,item in enumerate(all_author):
global t
if item in all_author[0:index]: #判断当前元素是否与之前元素重复 如果重复,则重命名
t=t+1
all_author[index] = item+str(t) #如多个重命名使作者加上字符 依次类推
#*************************************************** WebDriverWait(self.driver, 10).until(
ES.presence_of_element_located((By.XPATH, "//div[@class='text']//img"))
)
all_img = all_content[1].xpath("//div[@class='text']//img//@src") #图片列表
#解决有个张图片没有http:协议
for index,item in enumerate(all_img):
if 'http:' not in item:
all_img[index] = 'http:'+item dic = dict(zip(all_author,all_img)) #多个列表生产字典
#遍历字典保存图片
for key in dic:
hz = os.path.splitext(dic[key])[1] #取出后缀名.jpg/.png
filename = key+hz #文件名(标题+后缀名)
urllib.request.urlretrieve(dic[key],'images/'+filename) def main():
for i in range(9):
rea = Custer()
rea.start() if __name__ == '__main__':
main()
selenium爬取煎蛋网的更多相关文章
- Python Scrapy 爬取煎蛋网妹子图实例(一)
前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...
- python3爬虫爬取煎蛋网妹纸图片(上篇)
其实之前实现过这个功能,是使用selenium模拟浏览器页面点击来完成的,但是效率实际上相对来说较低.本次以解密参数来完成爬取的过程. 首先打开煎蛋网http://jandan.net/ooxx,查看 ...
- 爬虫实例——爬取煎蛋网OOXX频道(反反爬虫——伪装成浏览器)
煎蛋网在反爬虫方面做了不少工作,无法通过正常的方式爬取,比如用下面这段代码爬取无法得到我们想要的源代码. import requests url = 'http://jandan.net/ooxx' ...
- Python 爬虫 爬取 煎蛋网 图片
今天, 试着爬取了煎蛋网的图片. 用到的包: urllib.request os 分别使用几个函数,来控制下载的图片的页数,获取图片的网页,获取网页页数以及保存图片到本地.过程简单清晰明了 直接上源代 ...
- python爬虫–爬取煎蛋网妹子图片
前几天刚学了python网络编程,书里没什么实践项目,只好到网上找点东西做. 一直对爬虫很好奇,所以不妨从爬虫先入手吧. Python版本:3.6 这是我看的教程:Python - Jack -Cui ...
- scrapy从安装到爬取煎蛋网图片
下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/pip install wheelpip install lxmlpip install pyopens ...
- python3爬虫爬取煎蛋网妹纸图片(下篇)2018.6.25有效
分析完了真实图片链接地址,下面要做的就是写代码去实现了.想直接看源代码的可以点击这里 大致思路是:获取一个页面的的html---->使用正则表达式提取出图片hash值并进行base64解码--- ...
- Python Scrapy 爬取煎蛋网妹子图实例(二)
上篇已经介绍了 图片的爬取,后来觉得不太好,每次爬取的图片 都在一个文件下,不方便区分,且数据库中没有爬取的时间标识,不方便后续查看 数据时何时爬取的,所以这里进行了局部修改 修改一:修改爬虫执行方式 ...
- Python 爬取煎蛋网妹子图片
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...
随机推荐
- opencv debug版本在linux下编译,并写了一个DEMO
用如下方法编译opencv: git clone "https://github.com/opencv/opencv.git" mkdir opencv_debug cd open ...
- Django websocket 长连接使用
下载 pip install dwebsocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客 ...
- JS学习笔记Day22
一.Ajax的概念 (一)Ajax是一种在无需重新加载整个网页(刷新网页)的情况下能够更新部分网页的技术 (二)Ajax的全称是Asynchronous JavaScript and XML,即异步J ...
- Shell脚本统计文件行数
Shell脚本统计文件行数 转自 http://www.jb51.net/article/61943.htm 示例:row_count.sh文件 awk '{print NR}' row_cou ...
- 针对缓存在Redis中的聊天消息的持久化方案分析
选型依据 数据库的选型主要考虑一下几个方面: 数据库本身是否收费 数据库后期维护成本 是否支持水平及垂直扩展,及扩展的容易程度 业务数据本身特性 使用此数据库的开发成本 由于此数据库主要用来存储缓存在 ...
- 线段树——习题、lazy解析
习题: C. Cloud Computing lazy操作解析:
- 【译】使用 Flutter 实现跨平台移动端开发
作者: Mike Bluestein | 原文地址:[https://www.smashingmagazine.com/2018/06/google-flutter-mobile-developm ...
- 使用antd Table + mobx 处理数组 出现的一系列问题
在store中定义了一个数组: @observable list = [...] 若是在table组件中直接使用list: <Table className={styles.table} col ...
- mysql并发控制之MVCC
1.MVCC(Multiversion concurrency control) :多版本并发控制,当我们并发访问数据库(读或写)时,对事物内正在处理的数据做多版本控制,用以防止写操作的阻塞影响读操作 ...
- 很好用的电脑桌面远程控制软件 支持多平台 Win,Mac,Debian… 等操作系统 Anydesk
很好用的电脑桌面远程控制软件 支持多平台 Win,Mac,Debian, Ubuntu, FreeBSD… 等操作系统 Anydesk 官网下载地址:https://anydesk.com/remot ...