xpath的使用

XPath(全称:XML Path Language)即 XML 路径语言,它是一门在 XML 文档中查找信息的语言,最初被用来搜寻 XML 文档,同时它也适用于搜索 HTML 文档。因此,在爬虫过程中可以使用 XPath 来提取相应的数据。

1.xpath语法

/   根节点,节点分隔符,
//  任意位置
.   当前节点
..  父级节点
@   属性
*   任意元素
@*  任意属性
node()  任意子节点(元素,属性,内容)

xpath在python中的使用

from lxml import etree # 导包

html=etree.parse('search.html',etree.HTMLParser())
print(hrml.xpath('//*')) # 获取所有节点
print(html.xpath('//head')) # 指定节点(结果为列表)
print(html.xpath('//div/a')) # 子节点,子孙节点
print(html.xpath('//body//a[@href="image1.html"]/..')) # 父节点
print(html.xpath('//body//a[@href="image1.html"]')) # 属性匹配
print(html.xpath('//body//a[@href="image1.html"]/text()')) # 文本获取
print(html.xpath('//body//a/@href')) # 属性获取 
print(html.xpath('//body//a[1]/@id')) # 注意从1 开始取(不是从0)
print(html.xpath('//body//a[@class="li"]')) #  a 标签有多个class类,直接匹配就不可以了
print(html.xpath('//body//a[contains(@class,"li") or @name="items"]')) # 多属性匹配
print(html.xpath('//a[2]/text()')) # 按序选择
print(html.xpath('//a[last()]/@href')) # 取最后一个
print(html.xpath('//a[position()<3]/@href')) # 位置小于3的
print(html.xpath('//a/ancestor::*')) # ancestor:祖先节点 使用了* 获取所有祖先节点
print(html.xpath('//a/ancestor::div')) # 获取祖先节点中的div
print(html.xpath('//a[1]/attribute::*')) #  attribute:属性值 
print(html.xpath('//a[1]/child::*')) # child:直接子节点
print(html.xpath('//a[6]/descendant::*')) # descendant:所有子孙节点
print(html.xpath('//a[1]/following::*')) # following:当前节点之后所有节点
print(html.xpath('//a[1]/following-sibling::*')) # following-sibling:当前节点之后同级节点

2.selenium动作链

用来实现网站中有些严重需要按住鼠标滑动的效果

方式一:暴力的将图片移动到指定位置

actions=ActionChains(bro) #拿到动作链对象
actions.drag_and_drop(sourse,target) #把动作放到动作链中,准备串行执行
actions.perform()

方式二:图片一点一点的移动到指定位置

ActionChains(bro).click_and_hold(sourse).perform()
distance=target.location['x']-sourse.location['x']
    track=0
    while track < distance:
        ActionChains(bro).move_by_offset(xoffset=2,yoffset=0).perform()
        track+=2

通过动作链自动登录12306网站

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")  # 去掉自动化控制的提示
bro = webdriver.Chrome(executable_path='./chromedriver.exe', options=options)

bro.get('https://kyfw.12306.cn/otn/resources/login.html')

bro.maximize_window()
# 12306检测到了我们使用了selenium控制了浏览器,所以它的滑块出不来
bro.implicitly_wait(10)

try:
    username = bro.find_element(by=By.ID, value='J-userName')
    username.send_keys('') # 用户名
    password = bro.find_element(by=By.ID, value='J-password')
    password.send_keys('') # 密码
    time.sleep(3)
    btn = bro.find_element(by=By.ID, value='J-login')
    btn.click()
    span = bro.find_element(by=By.ID, value='nc_1_n1z')

    ActionChains(bro).click_and_hold(span).perform()  # 鼠标点主
    ActionChains(bro).move_by_offset(xoffset=300, yoffset=0).perform() #滑动

    time.sleep(10)

except Exception as e:
    print(e)

finally:
    bro.close()

3.使用打码平台自动登录

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from chaojiying import ChaojiyingClient
from PIL import Image
bro = webdriver.Chrome(executable_path='./chromedriver.exe')
bro.get('http://www.chaojiying.com/apiuser/login/')
bro.implicitly_wait(10)
bro.maximize_window()
try:
    username = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input')
    password = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input')
    code = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input')
    btn = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input')
    username.send_keys('')
    password.send_keys('')
    # 获取验证码:
    #1 整个页面截图
    bro.save_screenshot('main.png')
    # 2 使用pillow,从整个页面中截取出验证码图片 code.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')
    # 3 使用超级鹰破解
    chaojiying = ChaojiyingClient('', '', '')  # 用户中心>>软件ID 生成一个替换 96001
    im = open('code.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    print(chaojiying.PostPic(im, 1902))  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    res_code=chaojiying.PostPic(im, 1902)['pic_str']
    code.send_keys(res_code)
    time.sleep(5)
    btn.click()
    time.sleep(10)
except Exception as e:
    print(e)
finally:
    bro.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值