ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python爬虫---动作链,xpath的使用,打码平台使用,scrapy的介绍和安装,scrapy项目创建和启动

2022-08-03 20:02:19  阅读:241  来源: 互联网

标签:xpath body python driver 爬虫 scrapy html


目录

动作链(了解)


模拟按住鼠标拖动的效果,或者是在某个标签上的某个位置点击的效果,主要用来做验证码的破解(滑动验证码)

from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
driver.implicitly_wait(10)  # 使用隐式等待

driver.maximize_window()

try:
    driver.switch_to.frame('iframeResult') ##切换到iframeResult
    sourse=driver.find_element(By.ID,'draggable')
    target=driver.find_element(By.ID,'droppable')


    #方式一:基于同一个动作链串行执行
    # actions=ActionChains(driver) #拿到动作链对象
    # actions.drag_and_drop(sourse,target) #把动作放到动作链中,准备串行执行

    actions=ActionChains(driver).click_and_hold(sourse)
    actions.drag_and_drop_by_offset(target,10,20)
    actions.perform()
    #方式二:不同的动作链,每次移动的位移都不同

    # ActionChains(driver).click_and_hold(sourse).perform()
    # distance = target.location['x'] - sourse.location['x']  # 两个控件的x轴的距离
    # track=0
    # while track < distance:
    #     ActionChains(driver).move_by_offset(xoffset=2,yoffset=0).perform()
    #     track+=2
    # # ActionChains(driver).move_by_offset(xoffset=distance, yoffset=0).perform()
    # ActionChains(driver).release().perform()

    time.sleep(10)


finally:
    driver.close()


'''
总结:
    -ActionChains(driver).move_by_offset(xoffset=2,yoffset=0).perform()  滑块,滑动验证码的破解
    -actions.drag_and_drop_by_offset(target,10,20)  原来的12306的点选,cnblgs选出所有红绿灯的验证码
         actions=ActionChains(driver)
         actions.drag_and_drop_by_offset(target,10,20)

'''

# 12306 自动登录---》滑块显示不出来---》检测到咱们使用自动化测试软件控制了

xpath的使用


XPath 是一门在 XML 文档中查找信息的语言

一般解析库都会有子的的搜索标签的方法,一般都会支持css和xpath

使用

div:找div标签
/:找当前路径下的标签
//:找当前路径下子子孙孙下的标签
.:表示当前路径
..:表示上一层路径
@:表示取属性

案例

doc='''
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
   <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
  </div>
 </body>
</html>
'''
from lxml import etree

html=etree.HTML(doc)
# html=etree.parse('search.html',etree.HTMLParser())
# 1 所有节点
# a=html.xpath('//*')
# 2 指定节点(结果为列表)
# a=html.xpath('//head')
# 3 子节点,子孙节点
# a=html.xpath('//div/a')
# a=html.xpath('//body/a') #无数据
# a=html.xpath('//body//a')
# 4 父节点
# a=html.xpath('//body//a[@href="image1.html"]/..')
# a=html.xpath('//body//a[1]/..')
# 也可以这样
# a=html.xpath('//body//a[1]/parent::*')
# 5 属性匹配
# a=html.xpath('//body//a[@href="image1.html"]')

# 6 文本获取
# a=html.xpath('//body//a[@href="image1.html"]/text()')

# 7 属性获取
# a=html.xpath('//body//a/@href')
# # 注意从1 开始取(不是从0)
# a=html.xpath('//body//a[1]/@href')
# 8 属性多值匹配
#  a 标签有多个class类,直接匹配就不可以了,需要用contains
# a=html.xpath('//body//a[@class="li"]')
# a=html.xpath('//body//a[contains(@class,"li")]')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 9 多属性匹配
# a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
# # a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 10 按序选择
# a=html.xpath('//a[2]/text()')
# a=html.xpath('//a[2]/@href')
# 取最后一个
# a=html.xpath('//a[last()]/@href')
# 位置小于3的
# a=html.xpath('//a[position()<3]/@href')
# 倒数第二个
# a=html.xpath('//a[last()-2]/@href')
# 11 节点轴选择
# ancestor:祖先节点
# 使用了* 获取所有祖先节点
# a=html.xpath('//a/ancestor::*')
# # 获取祖先节点中的div
# a=html.xpath('//a/ancestor::div')
# attribute:属性值
# a=html.xpath('//a[1]/attribute::*')
# child:直接子节点
# a=html.xpath('//a[1]/child::*')
# descendant:所有子孙节点
# a=html.xpath('//a[6]/descendant::*')
# following:当前节点之后所有节点
# a=html.xpath('//a[1]/following::*')
# a=html.xpath('//a[1]/following::*[1]/@href')
# following-sibling:当前节点之后同级节点
# a=html.xpath('//a[1]/following-sibling::*')
# a=html.xpath('//a[1]/following-sibling::a')
# a=html.xpath('//a[1]/following-sibling::*[2]')
# a=html.xpath('//a[1]/following-sibling::*[2]/@href')

# print(a)

打码平台使用


验证码的破解

​ -简单的数字字母组合可以使用图像识别(python 现成模块),成功率不高
​ -使用第三方打码平台(破解验证码平台),花钱,把验证码图片给它,它给你识别完,返回给你

超级鹰的使用

from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image

bro = webdriver.Chrome()
bro.get('http://www.chaojiying.com/user/login/')
bro.maximize_window()

try:
    bro.save_screenshot('main.png')  # 把当前页面截图截图
    img = bro.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/div/img')
    location = img.location
    size = img.size
    print(location)
    print(size)
    # 使用pillow扣除大图中的验证码
    img_tu = (
    int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
    # # 抠出验证码
    # #打开
    img = Image.open('./main.png')
    # 抠图
    fram = img.crop(img_tu)
    # 截出来的小图
    fram.save('code.png')
    from chaojiying import ChaojiyingClient

    chaojiying = ChaojiyingClient('306334678', 'lqz12345', '937234')
    im = open('a.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    print(chaojiying.PostPic(im, 1902))

except:
    pass
finally:
    bro.close()

scrapy介绍和安装


Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速、简单、可扩展的方式从网站中提取所需的数据。但目前Scrapy的用途十分广泛,可用于如数据挖掘、监测和自动化测试等领域,也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫

专业爬虫:爬虫框架(scrapy),在固定位置写固定代码,就能完成爬虫的功能。

安装:

mac,linux:

pip3 install scrapy

win(上面的命令可能装不上):

1、pip3 install wheel #安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pythonlibs
2、pip3 install lxml
3、pip3 install pyopenssl
4、下载并安装pywin32:https://sourceforge.net/projects/pywin32/files/pywin32/
6、下载twisted的wheel文件:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
7、执行pip3 install 下载目录\Twisted-17.9.0-cp36-cp36m-win_amd64.whl
8、pip3 install scrapy

scrapy架构介绍


框架---》架构

1 spiders:爬虫(咱们的代码)
2 engin :引擎(大总管)
3 scheduler:调度器(排队,谁先爬谁后爬,去重)
4 downloader:下载器(真正的负责发送http请求,获取数据,性能很高,基于twisted,性能很高的网络框架)
5 piplines:管道(保存数据)

# 引擎(EGINE)
引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。

# 调度器(SCHEDULER)
用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址

# 下载器(DOWLOADER)
用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的

# 爬虫(SPIDERS)
SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求

# 项目管道(ITEM PIPLINES)
在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作


# 下载器中间件(Downloader Middlewares)
位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response,
# 爬虫中间件(Spider Middlewares)
位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests)

img

scrapy目录介绍,scrapy项目创建,爬虫创建,启动爬虫


scrapy是爬虫界的django

创建scrapy项目

scrapy startproject myfirstscrapy

创建爬虫

scrapy genspider cnblogs cnblogs.com

目录介绍

firstscrapy         # 项目名
    firstscrapy     # 文件夹
        spiders     # 文件夹,一个个的爬虫
            cnblogs.py # 其中一个爬虫,重点写代码的地方(解析数据,发起请求)*****
        items.py    # 类比djagno的models,表模型--》类         ***
        middlewares.py # 中间件:爬虫中间件和下载中间件都在里面  ***
        pipelines.py   # 管道,做持久化需要在这写代码           ***
        settings.py    # 配置文件                             **
    scrapy.cfg    # 上线配置,开发阶段不用

启动爬虫,爬取数据

scrapy crawl 爬虫名字  --nolog

或者在项目路径下新建main.py--->右键运行

from scrapy.cmdline import execute
execute(['scrapy','crawl','cnblogs','--nolog'])

标签:xpath,body,python,driver,爬虫,scrapy,html
来源: https://www.cnblogs.com/zaoan1207/p/16548533.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有