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爬取煎蛋网的更多相关文章

  1. Python Scrapy 爬取煎蛋网妹子图实例(一)

    前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...

  2. python3爬虫爬取煎蛋网妹纸图片(上篇)

    其实之前实现过这个功能,是使用selenium模拟浏览器页面点击来完成的,但是效率实际上相对来说较低.本次以解密参数来完成爬取的过程. 首先打开煎蛋网http://jandan.net/ooxx,查看 ...

  3. 爬虫实例——爬取煎蛋网OOXX频道(反反爬虫——伪装成浏览器)

    煎蛋网在反爬虫方面做了不少工作,无法通过正常的方式爬取,比如用下面这段代码爬取无法得到我们想要的源代码. import requests url = 'http://jandan.net/ooxx' ...

  4. Python 爬虫 爬取 煎蛋网 图片

    今天, 试着爬取了煎蛋网的图片. 用到的包: urllib.request os 分别使用几个函数,来控制下载的图片的页数,获取图片的网页,获取网页页数以及保存图片到本地.过程简单清晰明了 直接上源代 ...

  5. python爬虫–爬取煎蛋网妹子图片

    前几天刚学了python网络编程,书里没什么实践项目,只好到网上找点东西做. 一直对爬虫很好奇,所以不妨从爬虫先入手吧. Python版本:3.6 这是我看的教程:Python - Jack -Cui ...

  6. scrapy从安装到爬取煎蛋网图片

    下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/pip install wheelpip install lxmlpip install pyopens ...

  7. python3爬虫爬取煎蛋网妹纸图片(下篇)2018.6.25有效

    分析完了真实图片链接地址,下面要做的就是写代码去实现了.想直接看源代码的可以点击这里 大致思路是:获取一个页面的的html---->使用正则表达式提取出图片hash值并进行base64解码--- ...

  8. Python Scrapy 爬取煎蛋网妹子图实例(二)

    上篇已经介绍了 图片的爬取,后来觉得不太好,每次爬取的图片 都在一个文件下,不方便区分,且数据库中没有爬取的时间标识,不方便后续查看 数据时何时爬取的,所以这里进行了局部修改 修改一:修改爬虫执行方式 ...

  9. Python 爬取煎蛋网妹子图片

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...

随机推荐

  1. 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】

    若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...

  2. 高并发环境下全局id生成策略

    解决方案: 基于Redis的全局id生成策略:(推荐此方法) 基于雪花算法的全局id生成: https://www.cnblogs.com/kobe-qi/p/8761690.html 基于zooke ...

  3. (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  4. vue路由信息对象

    一个路由信息对象表示当前激活的路由的状态信息,每次成功的导航后都会产生一个新的对象. path字符串,对应当前路由的路径 params对象,包含动态路由参数 query对象,URL查询参数 hash字 ...

  5. 责任链模式-Chain of Responsibility(Java实现), 例1

    责任链模式-Chain of Responsibility, 例1 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. ...

  6. java使用redis数据库

    1.安装 Redis 支持 32 位和 64 位,根据实际情况选择不同的安装版本. 安装完成后打开命令提示窗口,切换到redis安装目录,启动redis客户端, redis-cli命令启动redis客 ...

  7. 模拟登陆github

    import requests from lxml import etree class Login(object): def __init__(self): self.headers = { 'Re ...

  8. Reinforcement Learning Solutions Ed2 Chapter 1 - 2 问题解答

    RL到了第三章题目多的不可思议 前两章比较简单,就在博客随便写写了.之后的用pdf更新. 1.1: Self-play will result different move even from the ...

  9. 在django admin中添加自定义视图

    来自https://blog.csdn.net/qq_35753140/article/details/84881757   django admin提供了完善的用户管理和数据模型管理,方便实用.研究 ...

  10. 【原创】大叔经验分享(48)oozie中通过shell执行impala

    oozie中通过shell执行impala,脚本如下: $ cat test_impala.sh #!/bin/sh /usr/bin/kinit -kt /tmp/impala.keytab imp ...