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 ...
随机推荐
- redis-cli 通过管道 --pipe 快速导入数据到redis中
最近有个需求,需要把五千万条数据批量写入redis中,方法倒是有很多种!效率最高的就是通过redis-cl管道的方式写入 一:先看看命令 cat redis.txt | redis-cli -h 12 ...
- 定向耦合器 Directional Couplers
microwave101,干货比较多 传送门:https://www.microwaves101.com/encyclopedias/directional-couplers Directional ...
- JS基础知识(未完)
学习笔记,知识点来源于掘金小册-yck-前端面试之道 1.原始类型(6种) 原始类型储存的都是值 number boolean string null undefined symbol null不是对 ...
- flink-conf.yaml
Flink 配置文件 对于管理员来说,差不多经常调整的就只有 conf 下的flink-conf.yaml : 经过初步的调整,大约有以下模块的参数(未优化) Licensed to the Apac ...
- I/O模型系列之三:IO通信模型BIO NIO AIO
一.传统的BIO 网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请 ...
- 树莓派安装 MySQL 时出现错误的解决方法
今天被要求解决一个树莓派无法正常安装 MySQL 的问题.以下是解决过程记录. 我在 Mac 上利用 SSH 连接到树莓派,执行 sudo apt-get install mysql-server m ...
- Python:Mac 下 MQTT 服务器 Mosquitto 的配置
我在Mac电脑上搭建时遇到了一些不同于网上大部分情况的问题,特此分享给可能也有遇到相同情况又找不到解决方法的人. 我的电脑系统:macOS Mojave 10.14.3. paho-mqtt 的安装 ...
- JavaScript数据类型 正则表达式RegExp类型
前言 上一篇文章已经介绍了正则表达式的基础语法.javascript的RegExp类表示正则表达式,String和RegExp都定义了方法,使用正则表达式可以进行强大的模式匹配和文本检索与替换.本文将 ...
- [转载] win10进行端口转发
1.添加端口转发netsh interface portproxy add v4tov4 listenport=4000 listenaddress=127.0.0.1 connectport=400 ...
- Aras简单报表
1.编辑Report对象类的窗体Report_Tab_Report,将xsl_stylesheet放到窗体上 2.新建报表 3.将编辑好的XSLT复制到xsl_stylesheet中. <xsl ...